Null object Reference

Yet another problem i face while i learn c# :face_with_spiral_eyes:. Ive narrowed down the problem area to this chunk of script.THe console reads Null object reference. Is there a mistake? I dont see one.

public void Check(string word){
		foreach(GameObject obj in array){
			if(word==obj.name){
				isword=true;
				currentobj=obj;
			}
			else{
				isword=false;
			}
		}
	}

The only thing in there I could see being null would be obj. the line that would be throwing the error is “if (word == obj.name)”.

Here’s how you find the error. Read up on try catch.

	public void Check (string word)
	{
		try
		{
			foreach (GameObject obj in array)
			{
				if (word == obj.name)
				{
					isword = true;
					currentobj = obj;
				} else
				{
					isword = false;
				}
			}
		} catch (Exception ex)
		{
			Debug.log ("Exception thrown: " + ex.message);
		}
	}

Now, put a breakpoint on that line “Debug.log”. Run your game from the debugger, and look at the array.

Ok thanks ill try that but how would if(word==obj.name) produce a Null Reference error? Arent those both strings?

If “obj” is null, it could certainly throw a null reference exception. I see that obj is coming from your “array” item. Where does “array” come from? Maybe whatever loaded “array” with objects added some null items?

You could also add something like this just inside the foreach loop…

if (obj == null) { Debug.Log("Obj is null!"); continue; }

If you see the new message in the console, the object you retrieved from your array is null.

its unlikely that the obj check ends on null as the enumerator shouldn’t pick objects that are no GameObjects. Chances though are that array is null and then it will fail due to this.

I have an array of GameObjects that I want the user to be able to type the name to see if they exist. So the array should be filled but right now the array only has 1 item and that is a basic cube. The only thing I dont understand is how word wont equal the name of the object.

if there are no game objects in array, that could be the start of it. first, put the print line in there and see if your object is null. When you confirm that it is, do the same thing where ‘array’ is assigned to see if the objects ever got in there in the first place.

public void Check(string word){
if(array == null) print ("array is null");
else print("array has a length of "+array.Count+" objects"); // it might be array.Length
		foreach(GameObject obj in array){
if(obj == null) print("obj is null");
else print("we got the obj");
			if(word==obj.name){
				isword=true;
				currentobj=obj;
			}
			else{
				isword=false;
			}
		}
	}

Thank you your messages helped me find the problem. Turns out I had a method that was defective and set the array length to a huge number. I just had to set break after currentobj=obj so that if i find a word it just stops the loop

If you’re just looking for the first match, then short-circuiting the loop is a good idea for speed purposes. However, it’s not really the right way to fix this issue as it’s really just bypassing the root of the problem.

You really should 1) ensure that your array is properly constructed and 2) verify that your object is not null before you attempt to access it.

Jeff