- Home /
NullReferenceException: Object reference not set to an instance of an object
Hi, i got this error when i execute the script.
NullReferenceException: Object reference not set to an instance of an object
LoginGUI.LoginItems (Int32 windowID) (at Assets/Script/Login/LoginGUI.cs:47)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:1395)
i don't really get it where it was wrong :(
so here's my code...
LoginGUI.cs
using UnityEngine;
using System.Collections;
public class LoginGUI : MonoBehaviour
{
string username = "";
string password = "";
// string registerURL = "localhost/register.php";
[HideInInspector]
public string problem = "";
LoginSystem loginSystem;
// Use this for initialization
void Start ()
{
Debug.Log("LoginGUI Initialized");
}
void OnGUI()
{
GUI.Window(0,new Rect(Screen.width/40, Screen.height / 40, Screen.width / 1.05f, Screen.height / 1.1f), LoginItems, "Login");
}
void LoginItems(int windowID)
{
GUI.Label(new Rect(Screen.width/ 5, Screen.height / 3.9f,375,30), "<-Username->");
{
username = GUI.TextField(new Rect(Screen.width/ 2.2f, Screen.height / 4, 375,30),username);
}
GUI.Label(new Rect(Screen.width/ 5, Screen.height / 2.15f,375,30), "<-Password->");
{
password = GUI.TextField(new Rect(Screen.width/ 2.2f, Screen.height / 2.2f,375,30), password);
}
if(GUI.Button(new Rect(Screen.width/ 5, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Login"))
{
loginSystem.LoginWWW();
}
if(GUI.Button(new Rect(Screen.width/ 1.7f, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Register"))
{
loginSystem.RegisterWWW();
}
GUI.Label (new Rect(Screen.width/ 2.3f, Screen.height / 1.85f,Screen.width/ 7, Screen.height / 15), problem);
}
public string getUsername()
{
return username;
}
public string getPassword()
{
return password;
}
}
LoginSystem.cs
using UnityEngine;
using System.Collections;
public class LoginSystem : MonoBehaviour
{
LoginGUI loginGUI;
string loginURL = "localhost/login.php";
string registerURL = "localhost/register.php";
// Use this for initialization
void Start ()
{
Debug.Log("LoginSystem Initialized");
}
public void LoginWWW()
{
StartCoroutine(LoginHandler(loginGUI.getUsername(), loginGUI.getPassword()));
}
public void RegisterWWW()
{
StartCoroutine(RegisterHandler(loginGUI.getUsername(),loginGUI.getPassword()));
}
IEnumerator LoginHandler(string usernameH, string passwordH)
{
loginGUI.problem = "Checking username and password...";
string loginURL_H = loginURL + "?username=" + usernameH + "&password=" + passwordH;
WWW loginReader = new WWW(loginURL_H);
yield return loginReader;
if(loginReader.error != null)
{
loginGUI.problem = "Cannot locate the page ! ";
}
else
{
if(loginReader.text == "right")
{
loginGUI.problem = "Logged in !";
}
else if(loginReader.text == "wrong")
{
loginGUI.problem = "username / password wrong !";
}
else
{
loginGUI.problem = "Internal error ! Contact the administrator !";
}
} StopCoroutine("LoginHandler");
}
IEnumerator RegisterHandler(string usernameH, string passwordH)
{
loginGUI.problem = "Registering..";
string registerURL_H = registerURL + "?username=" + usernameH + "&password=" + passwordH;
WWW registerReader = new WWW(registerURL_H);
yield return registerReader;
if(registerReader.error != null)
{
loginGUI.problem = "Cannot locate the page ! ";
}
else
{
if(registerReader.text == "registered")
{
loginGUI.problem = "Registered !";
}
else
{
loginGUI.problem = "Not registered ! if continue contact the administrator !";
}
} StopCoroutine("RegisterHandler");
}
}
i got the error in if(GUI.Button(new Rect(Screen.width/ 5, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Login")) { loginSystem.LoginWWW(); }
anyone know the problem ? any clue is really help me :(
Answer by KellyThomas · Dec 11, 2013 at 01:19 PM
As the exception message states a Null Reference
was found when running line 47:
loginSystem.RegisterWWW();
It looks like to are trying to call a method of loginSystem
but it is uninitialised i.e. has no instance of LoginSystem
assigned to it.
The easiest way to fix that is to assign a value in the inspector. Alternatively you could find an instance programmatically by using `GetComponant()`.
i have the function in LoginSystem public void RegisterWWW() { StartCoroutine(RegisterHandler(loginGUI.getUsername(),loginGUI.getPassword())); }
on LoginGUI.cs i am declaring it too
LoginSystem loginSystem;
so what's wrong with my script then ?
LoginSystem loginSystem;
Declares a variable name and type but does not assign a value. At this point you have a variable that holds a value of null
. You are unable to access any properties of a null variable, and if you try you will get a NullReferenceException
.
So we need to assign a value to this variable before we can use it to do any work.
You will have the LoginGUI
script attached to a GameObject in your scene. Use the inspector to look at this script and you will find a slot named "Login System" this is the inspector's representation of the loginSystem
variable. If you also have the LoginSystem script attached to a GameObject in your scene you can 1) click the little circle to browse and select; or 2) drag and drop, to assign a value to this slot.
If your needs are more complex and this assignment can not be made at design time it can be done programmatically at run time. An example of how this is done can be found over here at this other answer.