Reputation: 1003
I'm making a function that will get the number of lines from a StreamReader excluding comments (lines which starts with '//') and new lines.
This is my code:
private int GetPatchCount(StreamReader reader)
{
int count = 0;
while (reader.Peek() >= 0)
{
string line = reader.ReadLine();
if (!String.IsNullOrEmpty(line))
{
if ((line.Length > 1) && (!line.StartsWith("//")))
{
count++;
}
}
}
return count;
}
My StreamReader's data is:
// Test comment
But I'm getting an error, 'Object reference not set to an instance of an object.' Is there any way to fix this error?
EDIT Turns out this occurs when my StreamReader is null. So with musefan and Mr. Smith's suggested code, I came up with this:
private int GetPatchCount(StreamReader reader, int CurrentVersion)
{
int count = 0;
if (reader != null)
{
string line;
while ((line = reader.ReadLine()) != null)
if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
count++;
}
return count;
}
Thanks for the help!
Upvotes: 3
Views: 5391
Reputation: 1298
Your code is not enough information to resolve your problem. I have made a small application with VS 2010 base on my assume and it work well. I believe your code get problem with streamReader. If the streamReader is null your code will throw 'Object reference not set to an instance of an object.' You should check the streamReader is not null and make sure the streamReader are available.
You can reference code below. With make sure the TextFile1.txt existing at D:\ Hope this help.
namespace ConsoleApplication1
{
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
using (StreamReader streamReader = new StreamReader(@"D:\\TextFile1.txt"))
{
int count = GetPatchCount(streamReader);
Console.WriteLine("NUmber of // : {0}", count);
}
Console.ReadLine();
}
private static int GetPatchCount(StreamReader reader)
{
int count = 0;
while (reader.Peek() >= 0)
{
string line = reader.ReadLine();
if (!String.IsNullOrEmpty(line))
{
if ((line.Length > 1) && (!line.StartsWith("//")))
{
count++;
}
}
}
return count;
}
}
}
Upvotes: 0
Reputation: 4506
Slightly neater variant of musefan's suggested code; just a single ReadLine() code. +1 for suggesting the removal of the Length check btw.
private int GetPatchCount(StreamReader reader)
{
int count = 0;
string line;
while ((line = reader.ReadLine()) != null)
if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
count++;
return count;
}
Upvotes: 1
Reputation: 48415
There is no need to Peek()
, this may actually be the problem too. You can just do this:
string line = reader.ReadLine();
while (line != null)
{
if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
{
count++;
}
line = reader.ReadLine();
}
Of course if you StreamReader
is null then you have a problem, but you example code alone is not enough to determine this - you need to debug it. There should be plenty of debugging information for you to work out which object is actually null
Upvotes: 2
Reputation: 70728
Sounds like your reader
object is null
:
You can check if the reader is null by doing:
if (reader == null) {
reader = new StreamReader("C:\\FilePath\\File.txt");
}
Upvotes: 1