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.
Warning (active) CS8603 Possible null reference return. 65" i dont really understand what im missing tho.
10 Replies
well, firstly, those are not errors
it tells you it is a warning, not an error
please indicate which line is giving the warning
don't make us count line numbers
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
It's withing the whole loop condition.
I mean yeah.
But I'm marginally petty and want to teach good asking behavior.
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
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 nullGuys 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
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
}
Sorry for the late response. Using an array was a strict requirement for the project