[英]Null Reference exception in C#
I'm experiencing "null reference exception" when I'm attempting to return a value from a structure. 尝试从结构返回值时遇到“空引用异常”。
here is the code: 这是代码:
AssetItem item = new AssetItem();
item = initModified();
bool found = false;
int index = getIndex(barcode);
string modifiedFile = filepath + "Modified\\" + dir + "\\" + index + ".asdt";
if(File.Exists(modifiedFile))
{
using(StreamReader reader = new StreamReader(modifiedFile))
{
string line = reader.ReadLine();
while(line.Trim()!="")
{
string[] split = line.Split(',');
if(split[1]==barcode)
{
found = true;
break;
}
line = reader.ReadLine();
}
reader.Close();
}
}
if(found)
{
item.modified = true;
}
else
{
item.modified = false;
}
return item;
I initialize item by calling a sub containing that item.modified = false. 我通过调用包含该item.modified = false的子项来初始化item。 After checking that the file exist, I used a streamreader to read the lines of the file one by one until it finds a specific line and stops. 在检查文件是否存在之后,我使用了流读取器来逐行读取文件的行,直到找到特定的行并停止为止。 The problem is when it checked if the file exist and doesn't find the specific line. 问题是当它检查文件是否存在并且找不到特定行时。 It returns null even tough I initialize item to false and set it to false when it doesn't find the line. 即使我将item初始化为false,也很难返回它,否则返回null。 Note: this occurs seldom and works fine when I access other files to read and even in the same file that it returns null. 注意:这种情况很少发生,并且当我访问其他文件进行读取时,即使在返回null的同一文件中,也可以正常工作。
Note: Another problem I encountered is that it skips a line that it reads. 注意:我遇到的另一个问题是它跳过了读取的一行。
What could be the cause of this? 这可能是什么原因?
And the end of the file, ReadLine()
returns null - and you then call .Trim()
on it without checking (in the scenario where the item isn't there and you read the file all the way through) - hence you need to add a null-check (note also I've moved the ReadLine
so it happens consistently): 在文件末尾, ReadLine()
返回.Trim()
然后在不检查的情况下在其上调用.Trim()
(在项目不存在并且您一直读取文件的情况下)-因此,您需要添加一个空检查(请注意,我已经移动了ReadLine
因此它始终发生):
using(StreamReader reader = new StreamReader(modifiedFile))
{
string line;
while((line = reader.ReadLine()) != null && line.Trim() != "") {
...
}
}
Note that the above code (based on yours) will end on the first empty line; 请注意,以上代码(基于您的代码)将在第一个空行结束; personally I'd probably skip empty lines: 我个人可能会跳过空行:
using(StreamReader reader = new StreamReader(modifiedFile))
{
string line;
while((line = reader.ReadLine()) != null) {
if(line.Trim() == "") continue;
...
}
}
One problem that I can find in your code is that you don't need the following line: 我可以在您的代码中找到的一个问题是您不需要以下行:
reader.Close();
using
automatically does this for you. 自动using
会帮您做到这一点。
Furthermore, your looping condition should check EndOfStream
instead of trimming the line. 此外,循环条件应检查EndOfStream
而不是修剪行。
ie, Modifying your code to something like this: 即,将您的代码修改为如下所示:
using(StreamReader reader = new StreamReader(modifiedFile))
{
while(!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] split = line.Split(',');
if(split[1]==barcode)
{
found = true;
break;
}
}
}
On a side note, why create a new instance then re-assign to it without using it for any purpose.. 附带说明一下,为什么要创建一个新实例然后在没有任何用途的情况下重新分配它。
AssetItem item = new AssetItem();
item = initModified();
Could become 可能成为
AssetItem item = initModified();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.