Reputation: 860
I'm learning C and C#, this question is for C#. I can't see where and what is throwing the exception. Any help much appreciated?
Here is the code:
private static void geraNýjaNámsáætlun()
{
Console.Write("Hvada nám er þetta?:");
String nám = Console.ReadLine();
Console.Write("Villtu gera vikuáætlun? (y/n):");
string answerYesOrNo = Console.ReadLine();
answerYesOrNo.Trim();
string path = @"C:\nám";
if (answerYesOrNo.ToLower() == "y")
{
try
{
Console.Write("Enter the name you want for the filename:");
string some = Console.ReadLine();
string combined = Path.Combine(path, some + ".txt");
if (File.Exists(combined))
{
using (TextReader obj2 = new StreamReader(combined))
{
if (!obj2.ReadLine().Contains("Mon"))
{
obj2.Close();
TextWriter obj = File.AppendText(combined);
obj.WriteLine("Mon\t\t\t|Thue\t\t\t|Wedn\t\t\t|Thurs\t\t\t|Friday\t\t\t|Sat\t\t\t|Sun\t\t\t");
obj.Close();
}
}
}
using (TextWriter obj = File.AppendText(combined))
{
Console.WriteLine("Enginn fyrir monday 3 fyrir thuesday 6 fyrir wednesday 9 fyrir thursday 12 fyrir friday 15 saturday 18 fyrir sunday");
Console.Write("Enter the number of tabs:");
int numberOfTabs = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the class or lektion:");
string lektionOrClass = Console.ReadLine();
obj.WriteLine(Tabs(numberOfTabs) + "" + lektionOrClass);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
Now the exception thows here on this line, after I put a if check to see weather the ReadLine() is null. ?
Upvotes: 1
Views: 245
Reputation: 391326
The problem here is that you're using obj2.ReadLine()
twice.
if (obj2.ReadLine() != null)
{
if (obj2.ReadLine().Contains("Mon"))
This will attempt to read two lines from the file. However, if the first read reads the last line of the stream/file, the other call to ReadLine
will return null
, and that's your exception, when you try to call .Contains("Mon")
on a null-reference.
Basically, every time you call obj2.ReadLine()
, it will return the next line from the file, or null
if there are no more lines. Repeat calls to that method will not return the same line. To reuse the single/first line you read from the file, you need to store the result from the first call in a variable, and use the variable instead of calling the method again.
If you really want to consume two lines from the stream here, at least check if the line was actually read:
if (obj2.ReadLine() != null)
{
string line = obj2.ReadLine();
if (line != null && line.Contains("Mon"))
If you didn't want to consume two lines, change the code to this:
string line = obj2.ReadLine();
if (line != null && line.Contains("Mon"))
{
...
Upvotes: 3
Reputation: 4094
You're using the file outside of where you're checking for its existence; move it inside the block like so:
private static void geraNýjaNámsáætlun()
{
Console.Write("Hvada nám er þetta?:");
String nám = Console.ReadLine();
Console.Write("Villtu gera vikuáætlun? (y/n):");
string answerYesOrNo = Console.ReadLine();
answerYesOrNo.Trim();
string path = @"C:\nám";
if (answerYesOrNo.ToLower() == "y")
{
try
{
Console.Write("Enter the name you want for the filename:");
string some = Console.ReadLine();
string combined = Path.Combine(path, some + ".txt");
if (File.Exists(combined))
{
using (TextReader obj2 = new StreamReader(combined))
{
if (!obj2.ReadLine().Contains("Mon"))
{
obj2.Close();
TextWriter obj = File.AppendText(combined);
obj.WriteLine("Mon\t\t\t|Thue\t\t\t|Wedn\t\t\t|Thurs\t\t\t|Friday\t\t\t|Sat\t\t\t|Sun\t\t\t");
obj.Close();
}
}
using (TextWriter obj = File.AppendText(combined))
{
Console.WriteLine("Enginn fyrir monday 3 fyrir thuesday 6 fyrir wednesday 9 fyrir thursday 12 fyrir friday 15 saturday 18 fyrir sunday");
Console.Write("Enter the number of tabs:");
int numberOfTabs = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the class or lektion:");
string lektionOrClass = Console.ReadLine();
obj.WriteLine(Tabs(numberOfTabs) + "" + lektionOrClass);
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
Upvotes: 0
Reputation: 8841
As I Check string path = @"C:\nám";
path was exist on C drive, check this path exist or not.
Upvotes: 0
Reputation: 1663
Perhaps "obj2.ReadLine().Contains" cause this error, you can see it better running your project in "Debug" mode.
Upvotes: 0
Reputation: 3627
This error happens if object that is being assigned an Null value / Object is of not-assigned is being assigned to the newly created object. Please Debug using Breakpoints.
Upvotes: 0
Reputation: 1500145
I can't immediately tell you where the problem is, but I can tell you how to find out.
First, remove this catch block (and indeed the try
clause):
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
That is hiding valuable information from you - in particular the stack trace. Two important points:
IOException
due to an IO failure, which perhaps you can retry. NullReferenceException
is always due to a bug (usually in your own code) and you shouldn't try to handle that other than potentially at a very high level for a service which just needs to continue working.Once you've stopped catching the exception and throwing away useful information, you'll see where the NullReferenceException
is being thrown. At that point, you should be able to work out which null reference you're trying to dereference, and change the code appropriately.
If a single line is highlighted and you can't tell which reference might be null, that's often a symptom that you should refactor a complicated line of code into several simpler ones.
In fact, the problem may well be because you're reading from an empty file. In that case, obj2.ReadLine()
will return a null value (indicating the end of the file) and when you try to dereference that with your Contains
call, it will throw this exception. However, it's more important to understand the problem and how to diagnose it than to fix the immediate cause.
Upvotes: 4