我知道当NullReferenceException被抛出时,错误消息指出:
你调用的对象是空的.
我意识到当我尝试取消引用null对象引用时会抛出此消息.
错误消息暗示NullReferenceException除了对象引用之外可能还有一个原因null(可能是坏内存地址或类似内容).是这样的吗?
编辑:我更关心NullReferenceException可能抛出的原因而不是错误消息的措辞.错误消息的措辞正是提示问题的原因.
private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
XDocument xml = XDocument.Load(this.dir + xmlFile);
if (xml.Element(parent).Element(node).Value != null)
{
xml.Element(parent).Element(node).Value = newVal;
}
else
{
xml.Element(parent).Add(new XElement(node, newVal));
}
xml.Save(dir + xmlFile);
}
Run Code Online (Sandbox Code Playgroud)
为什么这会抛出
System.NullReferenceException未被用户代码处理
在这条线上
if (xml.Element(parent).Element(node).Value != null)
Run Code Online (Sandbox Code Playgroud)
?
我猜这是因为XML节点不存在,但这就是我们!= null要检查的内容.我该如何解决这个问题?
我已经尝试了几件事,他们在非空检查期间的某些时刻抛出相同的异常.
谢谢你的帮助.
.net c# exception-handling linq-to-xml nullreferenceexception
当方法可以获得空引用而不是有效的对象值时,拥有像 IllegalArgumentException 这样的自定义异常并在所有情况下抛出它是否是个好主意?
public void method(String str){
if(str == null)throw new CustomIllegalArgumentException("str cannot be null");
}
Run Code Online (Sandbox Code Playgroud)
我认为这样我总能看到这种非法参数异常和其他 RuntimeExceptions 之间的区别。
这主意好不好?
PS:我看过像Avoiding != null statements这样的帖子
**更新:**所以我会知道这是程序员意图的异常,我会有清晰的日志。
java null android nullpointerexception nullreferenceexception
我在.bin\Debug文件夹中添加了一个.txt文件,我试图打开它并像这样读取它:
using (StreamReader reader = File.OpenText("Credentials.txt")) {
string line = null;
do {
line = reader.ReadLine();
if (line.Contains("host=")) {
. . .
Run Code Online (Sandbox Code Playgroud)
但是,虽然文件在那里,当我到达"ReadLine()"行时,它会停止在其轨道中:
System.NullReferenceException未处理Message = Object引用未设置为对象的实例.
我不得不将它从" do...while (line != null);" 更改为" while (! reader.EndOfStream)"
在此事件处理程序中:
public static void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
DateTimeOffset timeStampUTC = args.Position.Coordinate.Timestamp.ToUniversalTime();
DateTimeOffset timeStampLocal = timeStampUTC.LocalDateTime;
DateTimeOffset dateTimeStampUTC = timeStampUTC.DateTime;
RecordLocation(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude,
args.Position.CivicAddress.City, args.Position.CivicAddress.State, dateTimeStampUTC, timeStampLocal);
}
Run Code Online (Sandbox Code Playgroud)
...我得到了一个Null Reference Exception因为args.Position.CivicAddress是null(其余的args传递给RecordLocation()有效).我估计有时会有位置null,有时它不会.如果没有城市或国家被发现的时间,我能做些什么呢?我试图使这些字符串RecordLocation()的定义可以为空,但这不会编译.
我是否需要检查CivicAddress是否为null并创建我的RecordLocation()方法的重载版本,还是有另一种方法来处理它?
c# geolocation coordinates nullreferenceexception windows-phone-8
我早些时候试图获得一些帮助,但我认为我没有提供足够的信息,尽管我很感谢所有的建议。
目标只是将对象室的新实例添加到数组中并打印到列表框。当用户尝试输入已经存在的房间名称时,它应该简单地显示在数组中已经存在的房间的规格中。
我不断收到空引用异常。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Room_Class
{
public partial class Form1 : Form
{
Room[] roomArray = new Room[20];
int count = 0;
public Form1()
{
InitializeComponent();
}
private void btnAddRm_Click(object sender, EventArgs e)
{
double length, width, height;
if (VerifyRoomName() == true)
{
if (txtRmLen.Text == "" || txtRmWid.Text == "" || txtRmHt.Text == "")
{
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我正在编写一个二十一点程序,但我的问题是创建一个具有其他对象列表的对象时的解决方案,但是当我首先创建对象时,列表为空.我想这就是我收到这个错误的原因.下面是我的代码,我将播放器初始化为在播放器的构造函数中有一个手的实例,但是当我调用方法playerHand.Hit(card)时,我得到"对象引用未设置为对象的实例"错误消息;
public class Player
{
private string name;
public Hand playerHand;
public Player()
{ }
public Player(string name)
{
this.Name = name;
this.playerHand = new Hand();
}
public string Name
{
get { return name; }
set { name = value; }
}
public void TakeCard(Card card)
{ playerHand.Hit(card); }`
Run Code Online (Sandbox Code Playgroud)
`
public class Hand
{
public List<Card> hand;
public Hand()
{
hand = new List<Card>();
}
public int handValue;`
public void Hit(Card card)
{
hand.Add(card);
}
public int HandValue()
{ …Run Code Online (Sandbox Code Playgroud) 我正在检查可能为空/空/空的列的单元格的单元格值,所以我需要一些东西来避免NullReferenceException.
我该怎么做,因为即使有IsNullOrWhiteSpace()并且IsNullOrEmpty()我以某种方式得到了那个例外。
这是我正在使用的代码的一部分:
s = "Total = " + dataGridView1.Rows[0].Cells.Count +
"0 = " + dataGridView1.Rows[0].Cells[0].Value.ToString() +
"/n 1 = " + dataGridView1.Rows[0].Cells[1].Value.ToString() +
"/n 2= " + dataGridView1.Rows[0].Cells[2].Value.ToString() +
"/n 3 = " + dataGridView1.Rows[0].Cells[3].Value.ToString() +
"/n 4= " + dataGridView1.Rows[0].Cells[4].Value.ToString() +
"/n 5 = " + dataGridView1.Rows[0].Cells[5].Value.ToString() +
"/n 6= " + dataGridView1.Rows[0].Cells[6].Value.ToString() +
"/n 7 = " + dataGridView1.Rows[0].Cells[7].Value.ToString();
if (string.IsNullOrEmpty(dataGridView1.Rows[0].Cells[8].Value.ToString()))
{
}
else
{
s += "/n 8 = " …Run Code Online (Sandbox Code Playgroud) 调用“sliderValue.Content = widthValue”(如下在滑块的 ValueChanged 事件处理程序中定义)时出现奇怪的空对象引用错误。
XAML:
<Window x:Class="StrangeError.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="166.066" Width="326.351">
<Grid>
<Slider x:Name="widthSlider" HorizontalAlignment="Left" Margin="6,56,0,0" VerticalAlignment="Top" Width="257" TickFrequency="10" SmallChange="0.01" TickPlacement="BottomRight" IsSnapToTickEnabled="True" Ticks="1, 2, 3, 4, 5, 6, 7, 8, 9, 10" ValueChanged="widthSlider_ValueChanged" Minimum="1"/>
<Label x:Name="sliderValue" Content="" HorizontalAlignment="Left" Margin="262,50,0,0" VerticalAlignment="Top"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
代码:
namespace StrangeError
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int widthValue;
public MainWindow()
{
InitializeComponent();
widthValue = 1;
sliderValue.Content = 1;
}
private void widthSlider_ValueChanged(object sender, …Run Code Online (Sandbox Code Playgroud) 从安全的角度来看,在释放内存后将指针设置为 NULL 是一种很好的做法。如果在释放内存之前将指针设置为 NULL 会发生什么?这将如何导致漏洞?
c pointers nullpointerexception nullreferenceexception dereference
c# ×8
.net ×2
android ×1
c ×1
coordinates ×1
dereference ×1
file-io ×1
geolocation ×1
java ×1
linq-to-xml ×1
null ×1
pointers ×1
string ×1
winforms ×1
wpf ×1