Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with error message: NullReferenceException

Discussion in 'Scripting' started by gfusco19, Sep 3, 2020.

  1. gfusco19

    gfusco19

    Joined:
    Jan 13, 2020
    Posts:
    5
    Hi everyone! I am trying to make a code where a text file with a list of stresses as decimal values is uploaded, split and converted into decimal variables that can be used in an if statement. The goal is to be able to look through these values and if any one of them exceeds a threshold (in this case 0.3), then a message will be given saying the component has failed. Below is the code that I have so far, however, when I run it I get the following error message:

    NullReferenceException: Object reference not set to an instance of an object
    loadTxt.Start () (at Assets/Scripts/loadTxt.cs:24)
    I am new to c# and am struggling to figure out the problem. If anyone would be able to assist me that would be great!

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Diagnostics;
    4. using UnityEngine;
    5.  
    6. public class loadTxt : MonoBehaviour
    7. {
    8.  
    9.     public string txtFile = "MYFILE3";
    10.     string txtContents;
    11.     decimal[] stress;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         char[] delimiterChars = { '\n' };
    17.         TextAsset txtAssets = (TextAsset)Resources.Load(txtFile);
    18.         txtContents = txtAssets.text;
    19.         string[] splitTxt = txtContents.Split(delimiterChars);
    20.  
    21.         for (var j = 0; j < splitTxt.Length; j++)
    22.             {
    23.                 decimal value = decimal.Parse(splitTxt[j]);
    24.                stress[j] = value;
    25.             }
    26.  
    27.         for (var k = 0; k < stress.Length; k++)
    28.             if (stress[k] > 0.3m)
    29.                 {
    30.                     UnityEngine.Debug.Log("Component Failed");
    31.                 }
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.      
    39.     }
    40. }
    41.  
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    You forgot to allocate memory of your array.
    Code (CSharp):
    1. stress = new decimal[neededLength];
     
    gfusco19 and Joe-Censored like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.
     
    Joe-Censored likes this.
  4. gfusco19

    gfusco19

    Joined:
    Jan 13, 2020
    Posts:
    5
    Thank you this fixed my error! However, now I am receiving a new error message "FormatException: Input string was not in a correct format." Do you have any suggestions on how to fix this? The text file I currently have uploaded is just a column of decimal values.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Don't use
    decimal.Parse()
    ... instead use
    if (decimal.TryParse(...))
    because it won't throw exceptions on bad data.

    All that said, if your data is bad, it will still fail to parse, returning false to the above if statement. Check that your data lines do not have newlines or carriage returns or other whitespace before or after them.

    Before trying to parse, do this:

    Code (csharp):
    1. Debug.Log( "My splitTxt is :[" + splitTxt + "]");
    and those square brackets can possibly help you identify if there is whitespace in there before or after, such as a newline or space or tab.
     
    gfusco19 likes this.
  6. gfusco19

    gfusco19

    Joined:
    Jan 13, 2020
    Posts:
    5

    Thank you so much! My code is working now