Null Reference exception in C#

后端 未结 3 2012
慢半拍i
慢半拍i 2021-01-25 09:17

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(         


        
3条回答
  •  终归单人心
    2021-01-25 09:58

    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.

    Furthermore, your looping condition should check EndOfStream instead of trimming the line.

    i.e., 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;
            }
        }
    }
    

提交回复
热议问题