0
 /*
 * loading the record file
 */
private void LoadRecordFile(int selectedRecordFile)
{

    Debug.Log(selectedRecordFile.ToString());

    string path = recordFilesArray[selectedRecordFile];

    Debug.Log(path);

    string text = " ";
    StreamReader inputStream = new StreamReader(path);
    char[] delimiter = { ',' };

    while (!text.Equals(null))
    {

        inputStream.ReadLine();

        Debug.Log(text);
        text = inputStream.ReadLine().Replace("(", "").Replace(")", "");

        string[] values = text.Split(delimiter);

        Vector3 vec3;
        Quaternion quaternion;

        vec3.x = float.Parse(values[0]);
        vec3.y = float.Parse(values[1]);
        vec3.z = float.Parse(values[2]);

        quaternion.x = float.Parse(values[3]);
        quaternion.y = float.Parse(values[4]);
        quaternion.z = float.Parse(values[5]);
        quaternion.w = float.Parse(values[6]);

        CarPositionList.Add(vec3);
        CarRotationList.Add(quaternion);

    }

    inputStream.Close();
}

I have this method to read log files. Every time I try to load a log file into my game I get a NullRefernceExcepton:

NullReferenceException: Object reference not set to an instance of an object
CarGameScript.LoadRecordFile (System.Int32 selectedRecordFile) (at 
Assets/Scripts/CarGameScript.cs:455)

I alrdy checked if the int value "selectedRecordFile" is null, but it's not. Normally, it should load the lofgfile without an error. The line 455 is:

 text = inputStream.ReadLine().Replace("(", "").Replace(")", "");

Any suggestions why am I getting this error?

Sascha
  • 95
  • 6
  • "I alrdy checked if the int value "selectedRecordFile" is null" which means nothing, as an `int` can´t be `null`. However there are plenty of opportunities anything different may be `null`, here, yopu best use your debugger to see exactly where the exception ocurs. – HimBromBeere Jan 30 '20 at 11:17
  • 1
    If `Quaternion ` is a class, `quaternion.x` surely will throw, as `quaternion ` has not been initialized and `null.x` makes no sense. – HimBromBeere Jan 30 '20 at 11:19
  • 1
    I initilized it but it unfortunately changed nothing – Sascha Jan 30 '20 at 11:41
  • I do not see any initialation of `quaternion`. Aynway, maybe any of your two lists is null. Just use your **debugger**. – HimBromBeere Jan 30 '20 at 11:58
  • 1
    There is no initialization needed. I found the solution. I changed the boolean in the while loop to "!inputStream.EndOfStream" . But I dont know why ? – Sascha Jan 30 '20 at 12:01
  • 1
    And why do u marked my question as duplicate ? The question u linked answers the nullreferencequestion in generel for C#. But my question is focused on unity + c# – Sascha Jan 30 '20 at 12:14
  • I think Sascha is right – heinrichpauli Jan 30 '20 at 12:21
  • Why do you think an NRE in unity is any different from an NRE in C#? In either way the way to identify the exception is by using your debugger appropriately. – HimBromBeere Jan 30 '20 at 12:43
  • `selectedRecordFile` can never be `null`, it is an `int`. The next obvious options would be in this order `recordFilesArray`, `path`, `inputStream`. And also note: Your code is not save for a malformatted input file! What happens if a line in your file simply does not have enough `,`? You try to access `values[0]` up to `values[6]` without even checking if `values.Length == 7` before ... – derHugo Jan 30 '20 at 13:47
  • @derHugo I guess `values[6]` would throw an `IndexOutOfBoundsException`, or does `Split` return `null`-elements? Doubt so. `InputStream` on the other hand gets its value assigned above the loop. – HimBromBeere Jan 30 '20 at 14:41
  • @HimBromBeere this last sentence was started with `also note` and wasn't mend to be related to the exception but an additional note ;) I don't know how `new StreamReader(path)` reacts to an empty or malformed path ... but yeah probably a different exception as well – derHugo Jan 30 '20 at 14:42
  • thanks for the additinal comments, but I alrdy wrote the answer in my comment 5 hours ago @ derhugo thanks for this hint. But I have a final format for the log file, which gets created by me. @ HimBromBeere u are right, but my problem is more specific and not a duplicate of that question. I'm not asking what a NRE is, i'm asking why I have one in this particular situation. But it is still only my opinion. – Sascha Jan 30 '20 at 19:11

0 Answers0