- Home /
NullReferenceException on function call
Hello unity community, I know this question is asked alot and I really have tried to find the answer I'm looking for, but I just don't know what to search for or where to look. I just can't get away from this exception:
NullReferenceException: Object reference not set to an instance of an object LoginScript+c__Iterator2.MoveNext () (at Assets/Scripts/LoginScript.cs:91)
All needed variables are public.
All classes have the following applied:
public static ClassName Instance
{
get
{
return _instance;
}
}
private static ClassName _instance;
This is the function that gives me the exception:
IEnumerator login(WWW w)
{
message += "Logging in..";
yield return w;
if (w.error == null)
{
if (w.text == "Success")
{
loggedin = true;
message = "";
status = "Online";
AppManager.Instance.UpdateStatus(); <-- This is where I get the null reference exception
}
else
{
message += w.text;
}
}
else
{
message += "ERROR: " + w.error + "\n";
}
}
The UpdateStatus() function looks like this:
public void UpdateStatus()
{
WWWForm updateform = new WWWForm();
updateform.AddField("action", "updatestatus");
updateform.AddField("token", LoginScript.Instance.token);
updateform.AddField("user", LoginScript.Instance.user);
updateform.AddField("status", LoginScript.Instance.status);
updateform.AddField("pos", "");
WWW ww = new WWW(LoginScript.Instance.usermanagerpage, updateform);
StartCoroutine(SendDataToDatabase(ww));
}
Thanks in advance!
The error message is in LoginScript, which makes me think that App$$anonymous$$anager.Instance
is null. Have you checked for that?
If you're not sure what that means, you should look up some tutorials that explain what null
and NullReferenceException
are.
I have this piece of code in the App$$anonymous$$anager class though?
void Start()
{
if(_instance != this)
{
if(_instance == null)
{
_instance = this;
}
}
}
Can you guarantee that gets called before LoginScript does its thing? $$anonymous$$any singletons use a "lazy instantiation" pattern, where they're created on-demand the first time they're needed. This is a little bit more coding, but it makes sure the Whatever$$anonymous$$anager is always available.
I do the code above in initialization, before I define the variables, so it's the very first thing that gets defined.
Answer by dsada · Sep 08, 2014 at 08:48 PM
Try to make the singleton class like this:
public class AnyClass : MonoBehaviour
{
private static AnyClass _instance;
public static AnyClass GetInstance()
{
if(_instance == null)
{
GameObject obj = new GameObject();
_instance = obj.AddComponent<AnyClass>();
}
return _instance;
}
}
also it is pretty much the same if the singleton class doensnt have to be a MonoBehaviour:
public class AnyClass
{
private static AnyClass _instance;
public static AnyClass GetInstance()
{
if(_instance == null)
{
_instance = new AnyClass();
}
return _instance;
}
}
In both cases you can use it anywhere like this:
AnyClass.GetInstance().DoWhatever();
And with this strategy you can be absolutetly sure that the instance is not null when you are using it.
This seems to have solved the NullReferenceException. Thanks! I don't quite understand why the method I used didn't work, can anyone explain that to me, that would be awesome :) But it runs without further NullReferenceExceptions for now :D
Because you either didnt give a value for your _instance variable at all or the App$$anonymous$$anager.Instance.UpdateStatus(); line was running before you gave a value to it.
Why does it have to create a new game object? Can't it read the script that is already in the scene? Or can't the scripts access scripts attached to the camera?
It is not neccessary to create a new GameObject. If you have a GameObject to attach you can use that of course. Since i would have liked to write a short code this seemed O$$anonymous$$ to me.