NullReferenceException: Object reference not set to an instance of an object (89487)

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 :frowning:

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 :frowning:

1 Answer

1

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 ?