- Home /
Why Null Reference Exception here?
I've looked at quite a few script examples here in "Answers" and it looks like I'm doing things correctly but I get a "Null Reference Exception" on line 26.
Background. When I run my program, the "Start" here will print the correctly so I have the UI correctly added to the public Text object. When I run my program, I access this from another script and the function ShowDataCity() is properly accessed (?) because the Debug Log correctly prints "City Data !!!". BUT, then I get an error on the very next line. It seems as though since it works fine in the Start function it should also work in the ShowDataCity() function. Hmmmm.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DisplayUIInfo : MonoBehaviour
{
public Text testInfoData;
private string tempText;
private void Start()
{
testInfoData.text = "Start !!!";
}
public void ShowDataSingleUnit()
{
Debug.Log("Single Unit Data !!!");
}
public void ShowDataCity()
{
string stuff = "City Data !!!";
Debug.Log(stuff);
testInfoData.text = stuff; ///<<<< Null Reference Exception
}
public void ShowDataRawMats()
{
Debug.Log("Raw Material Data !!!");
}
}
Line 14, where it displays "Start !!!" works fine. But line 26 gets the error. The program runs fine when I start it but when I click on one of my cities I get the error. Even though I get here from another Script, it seems as though it should work since I'm not passing any data from the other script to here.
What have you done to debug your problem? Have you actually tried checking "testInfoData" for a null value inside "ShowDataCity"? $$anonymous$$aybe you set it to null from somewhere else? If not we just need more information. For example the exact stacktrace. Is the exception thrown by that line or from inside the text property setter? Are you sure you don't call that method from another thread?
Thanks. I'm still working on this and I appreciate your suggestions and will continue to exa$$anonymous$$e the problem. Funny thing, if I call "ShowDataCity" from the start function of the same script, it will properly display the info without any error. I get the NullRefExeption error only when I call "ShowDataCity" from another script. Oddly, I get the error even when I'm not passing any information from the other script. Again, I'm still looking at this.
Clearly testInfoData is being destroyed or sth. at some point. You better track its activity in update
Answer by Topthink · Mar 07, 2019 at 08:43 PM
I figured out a "Work Around" for this problem. I've been trying to call a function on one script from another script but I kept getting a NullReferenceException and I couldn't understand why.
...declared on ScriptB...
public ScriptA callFunctionFromA;
...called from ScriptB...
callFunctionFromA FunctionName();
The above had always worked for me in the past but in this case I was getting the NullReferenceException and I'm not sure what I was doing to cause it. In any event I changed how I accessed the other script and it now works as designed.
...declared on ScriptB…
private ScriptA _scriptA;
...added to "Start" in ScriptB…
_scriptA = FindObjectOfType<ScriptA>();
...called from ScriptB…
_scriptA.FunctionName();
Both are fairly standard methods of accessing another script but I normally use the first because it apparently uses less time to work. I've never had a problem with the first method until this last program and again, I'm not sure what I was doing that caused the problem this time around. But using the second method works fine here and I will go ahead and use it for now and try to figure out the specific problem as I gain more knowledge about debugging, etc.
Answer by CCnockaert · Mar 06, 2019 at 04:58 PM
Something is destroying the testInfoData Text Object OR, something is breaking the reference to it.
You should replace this line :
public Text testInfoData;
--> [SerializeField] Text testInfoData;
It will prevent other scripts to break the reference. https://docs.unity3d.com/ScriptReference/SerializeField.html
Thank you, I'll try what you are suggesting. I appreciate that you took the time to help me.
This did not work for my situation although, as you point out, it may well work for other similar situations. Thanks for your input, however.
Answer by NikLaws · Mar 07, 2019 at 08:24 AM
Had the same issue. Fixed it by checking if it's not null.
if(testInfoData != null) testInfoData.txt = stuff;
it was answered in another thread about NullReferenceException.
Thank you. I will try that immediately and see how that impacts my program. I'm still working on debugging this matter as I get the time but I'm not an expert at debugging and it takes me time. I did look through many threads about this matter and didn't see what you indicate although I fully believe what you say and I really appreciate your assistance.
The code you suggest here does indeed prevent the error but the underlying problem still remains which is that I need the info to display. I need to either figure out what is causing the info to be lost or destroyed (I'm still working on this) or I need to figure out an alternate method to get the data to display (I'm working on this as well). I'm not an expert debugger so it's taking me some time to work through all this.
Again, thanks for your help.
Your answer
Follow this Question
Related Questions
4.6 UI Text rect does not expand automatically 2 Answers
uGUI letter spacing and kerning 1 Answer
Missing canvas elements on Build & Run. 2 Answers
How to change color of animated Text component 2 Answers
Inputfield text to String variable 1 Answer