C
C#2mo ago
TimVibes

Null Literal and NUll reference problems

Hey guys, Beginer at using Files here. Im running into 2 errors while trying to read the contents of a text file, then putting the contents into a consructor. "CS8600 Converting null literal or possible null value to non-nullable type.35
Warning (active) CS8603 Possible null reference return. 65" i dont really understand what im missing tho.
public static Employees[] EmployeeRead(string Filename)
{
try
{
FileInfo FileProperty = new FileInfo(Filename);
Console.WriteLine($"File name: {FileProperty.Name}");

FileStream file = new FileStream(Filename, FileMode.Open, FileAccess.Read);
StreamReader text = new StreamReader(file);

try
{
Employees[] employees = new Employees[13];

string line;
int LineCount = 0;


while ((line = text.ReadLine()) != null) {

employees[LineCount] = new Employees(line, Convert.ToInt32(line[1]), Convert.ToDecimal(line[2]), Convert.ToDouble(line[3]));
LineCount++;
}


foreach (Employees employee in employees) {
employee.ToString();
}
text.Close();
return employees;


} catch (InvalidCastException)
{
Console.WriteLine("Some bad conversion");


}catch (IOException)
{
Console.WriteLine("idk");

}

}catch (IOException)
{
Console.WriteLine("Smth wrong with the file path or reading file");

}
return null;
}
public static Employees[] EmployeeRead(string Filename)
{
try
{
FileInfo FileProperty = new FileInfo(Filename);
Console.WriteLine($"File name: {FileProperty.Name}");

FileStream file = new FileStream(Filename, FileMode.Open, FileAccess.Read);
StreamReader text = new StreamReader(file);

try
{
Employees[] employees = new Employees[13];

string line;
int LineCount = 0;


while ((line = text.ReadLine()) != null) {

employees[LineCount] = new Employees(line, Convert.ToInt32(line[1]), Convert.ToDecimal(line[2]), Convert.ToDouble(line[3]));
LineCount++;
}


foreach (Employees employee in employees) {
employee.ToString();
}
text.Close();
return employees;


} catch (InvalidCastException)
{
Console.WriteLine("Some bad conversion");


}catch (IOException)
{
Console.WriteLine("idk");

}

}catch (IOException)
{
Console.WriteLine("Smth wrong with the file path or reading file");

}
return null;
}
10 Replies
reflectronic
reflectronic2mo ago
well, firstly, those are not errors it tells you it is a warning, not an error
jcotton42
jcotton422mo ago
please indicate which line is giving the warning don't make us count line numbers
reflectronic
reflectronic2mo ago
i mean, the error is certainly the line return null;, because the function returns Employees[] an Employees[] "should not" be null, which is why you are getting the warning it would like you to return Employees[]? instead, which indicates that it may be null
TimVibes
TimVibes2mo ago
It's withing the whole loop condition.
jcotton42
jcotton422mo ago
I mean yeah. But I'm marginally petty and want to teach good asking behavior.
TimVibes
TimVibes2mo ago
I meant for it to return null if the employee file wasn't read of object not initialised Is there a way to format code better on this GC? The java GC does formating pretty well
reflectronic
reflectronic2mo ago
the loop condition is the same deal, you are saying line is never null, but you assign ReadLine() to it, which could be null. it wants you to write string? line instead, to show that line could be null
TimVibes
TimVibes2mo ago
Guys can you take a look at the while loop. Trouble begun over there, even while disregarding the final return null statement I also tried the string? Line but it was still bringing me problems I'll get home and try again tho
Bailey
Bailey2mo ago
Hi, I'm looking at the code and there can be a number of causes. Hope you can use the following: I would replace the array with a list like: List<Employees> listEmployees = new List<Employees>(); and add items using: listEmployees.Add(new Employees(line, Convert.ToInt32(line[1]), Convert.ToDecimal(line[2]), Convert.ToDouble(line[3]))); the second part is about ul reference. What you do in the code: employees[LineCount] = new Employees(line, Convert.ToInt32(line[1]), Convert.ToDecimal(line[2]), Convert.ToDouble(line[3])); - convert the second char into integer - convert the third character into decimal - convert the fourth char into double. When the line does not have the chars, the array (a string is under the hood an array of chars) it will give a null reference error. There are more possible tests you can add but first start something like below within the loop. if (string.IsNullOrEmpty(line) || line.Length <5 ) { // give some error or message and put breakpoint to look at data }
TimVibes
TimVibes2mo ago
Sorry for the late response. Using an array was a strict requirement for the project
Want results from more Discord servers?
Add your server