0

I am trying to display a string from a csv file as a text mesh pro text. However, I get NullReferenceException.

I checked and I've draged a text mesh pro texts into the into the appropriate places in the inspector.

public TMP_Text NameText;

string[] files = Directory.GetDirectories(path);

foreach (string file in files)
{
    string[] csvLines = File.ReadAllLines($@"{file}\info.csv");
    foreach (string line in csvLines)
    {
        CInfo CI = new CInfo(line);

        NameText.text = $"loaded {CI.name}";
    }
}

if I try Debug.log(CI.Name);, it works just fine.

I have another public text mesh pro text just one line above the NameText and it works perfectly fine. The NameText on the other hand, gives a null reference exception.

public TMP_Text LocationText; //this one works
public TMP_Text NameText; // this one gives a NullReferenceException: Object reference not set to an instance of an object

The 2 public texts

They are both set to a text mesh pro text in the inspector so there shouldn't be any reason for just the NameText to give a null exception error.

NameText.text = "something" aslo gives a null reference exception

  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Aug 29 '22 at 09:00
  • Did you try fixing it in the debugger? –  Aug 29 '22 at 09:01
  • @MickyD, not realy, since i have another ext mesh pro text that displays the location of the file. it is also public and it is just one line above the `NameText`. And yet, it works perfectly fine. –  Aug 29 '22 at 09:07
  • Did you assign the name text? – BugFinder Aug 29 '22 at 09:33
  • @BugFinder, yes, I did. –  Aug 29 '22 at 09:34
  • Its telling you from your updated text. It is not assigned. So for some reason either you destroy the gameobject or its not assigned. – BugFinder Aug 29 '22 at 10:36
  • Please consider using `TextMeshProUGUI` instead – zain ul din Aug 29 '22 at 11:19

1 Answers1

0

Based on the Inspector screenshot the reference is fine. Possible explanations for the exception include:

  1. The reference somehow gets set to null at runtime.
  2. There are more than one instance of the same component in the scene.
  3. The null reference exception doesn't actually come from the NameText component.

You can pass this as context to Debug.Log and then click on the Console message to make sure the message is coming from the instance you're expecting it to come from.

You can also log to the Console whether or not NameText equals null on the row before the NullReferenceException, to make sure the NameText is indeed the culprit.

foreach(string line in csvLines)
{
    CInfo CI = new CInfo(line);
    
    Debug.Log(@"NameText of {name} is null: {NameText == null}", this);

    NameText.text = $"loaded {CI.name}";
}

You can also log whether or not NameText is null during the Awake event function, to help you understand if the value potentially starts off not null but is set to null later on.

Sisus
  • 651
  • 4
  • 8