NullReferenceException (122315)

I understand the following error message is caused when (in general) Unity is telling me that one of the things in line 54 of the myProg script doesn’t exist:

NullReferenceException: Object reference not set to an instance of an object
myProg.OnGUI () (at Assets/Scripts/Editors/myProg.cs:54)

But as far as I can see and figure, there is nothing missing there in any way…

Here is the relevant part of the code snippet, and line 54 is the bit with .enabled (middle line):

if( GUILayout.Button("SART07Johnson") )
	{
		stage.setFatigueInductor( new Sart07JohnsonDefinition(GetComponent<ExperimentCreator>().getExperiment().getAvailableTableName("SART07Johnson")) );
		stage.setTypeOfFI( FatigueType.SART07Johnson );
		SART07JohnsonEditor editorSart07Johnson = GetComponent<SART07JohnsonEditor>();
		editorSart07Johnson.enabled = true;
		editorSart07Johnson.Init( (Sart07JohnsonDefinition)stage.getFatigueInductor(), this.stage.StageName,GetComponent<ExperimentCreator>().getExperiment().GetExperimentName() );
		GetComponent<NavigationTree>().CurrentEditor = editorSart07Johnson;
		this.enabled = false;
	}

So, I am wondering… what else could be the problem? Any specific other reason that I could look into, that might be causing this error message to be generated?

EDIT: GetComponent calls SART07JohnsonEditor which means my game object now has SART07JohnsonEditor attached? Is that not right?

We need to see your script to give you specific information. Without the script, we'd just be guessing.

Thank you. I added EDIT.

Try replacing the code with the following: if(editorSart07Johnson==null) { Debug.Log("Null. Hmmm"); } else { editorSart07Johnson.enabled = true; } If the error message appears, then the problem will be that the GetComponent will have failed. The likely cause of that being that the component was not successfully added in the first place. Let us know how it went EDIT: GetComponent does not add a component

Thank you. I realized the component was not successfully added in the first place...

1 Answer

1

EDIT: Well, I figured out that you meant to say AddComponent.