Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Can anyone tell me why this program compiles OK but throws an exception at runtime.

Discussion in 'Programming Questions and Suggestions' started by BumbleGrum, Jan 26, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. BumbleGrum Trainee Engineer

    Messages:
    11
    The following program monitors a number of conveyor sorters and turns 'DrainAll' on or off depending on the supplied arg to the main function.

    The script compiles, but throws an exception when I try to change the DrainAll value. The exception says that the "Object reference does not point to an instance of an object".

    I'm sure I've done something ultimately dumb, but I am a relative newbie.

    [Update] 26/01/18
    Found the problem! The wiki doesn't list the DrainAll action as a toggle, but it is. I simply changed the action to "DrainAll" and the script now runs as expected.


    Here's the code:

    Code:
    // AutoDrain v1.00
    // BumbleGrum 24/01/2018
    
    // A script to monitor all inbound DrainAll required Sorters on a grid and toggle them
    // on or off depending on the arg supplied "TurnOn" or "TurnOff".
    // To have a sorter monitored, simply place ACTIONABLE_ID somewhere in the sorter's name.
    // Any number of sorters can be monitored this way.
    const string ACTIONABLE_ID = "[to container]";
    const string DEBUG_PANEL = "DebugPanel";
    IMyTextPanel debug_panel = null;
    
    void Main(string arg)
    {
    	if	(arg != null)
    	{
    		bool turnOn = false;
    		if (arg == "TurnOn")
    		{
    			turnOn = true;
    		}
    		
    		debug_panel = GridTerminalSystem.GetBlockWithName(DEBUG_PANEL) as IMyTextPanel;	
    		// Initialize Debug
    		debug("", false);
    		debug("arg = " + arg, true);	
    		
    		List<IMyTerminalBlock> sorters = new List<IMyTerminalBlock>();
    		GridTerminalSystem.GetBlocksOfType<IMyConveyorSorter>(sorters);
    		
    		for (int i = 0; i < sorters.Count; i++)
    		{
    			if (sorters[i].CustomName.Contains(ACTIONABLE_ID))
    			{
    				// Get the sorter name (if any)
    				IMyConveyorSorter thisSorter = sorters[i] as IMyConveyorSorter;
    				int rootEnd = thisSorter.CustomName.IndexOf(ACTIONABLE_ID);
    				string nameRoot = thisSorter.CustomName.Remove(rootEnd);
    				debug("nameRoot: '" + nameRoot + "'");
    				// See if it has any data.
    				int dataStart = thisSorter.CustomName.IndexOf(ACTIONABLE_ID);
    				debug("dataStart: '" + dataStart.ToString() + "'");
    				if (dataStart >= 0)
    				{
    					debug("turnOn = " + turnOn);
    					if (thisSorter.DrainAll) // DrainAll is On
    					{
    						debug("Sorter: " + nameRoot + " DrainAll is 'ON'", true);
    						if (!turnOn)
    						{
    							debug("DrainAll will be turned OFF");
    
    							// Following statement throws an exception:
    							// "Object reference not set to the instance of an object"
    
    							thisSorter.GetActionWithName("DrainAll_Off").Apply(thisSorter);
    						}
    					}
    					else  if (!thisSorter.DrainAll) // DrainAll is Off
    					{
    						debug("Sorter: " + nameRoot + " DrainAll is 'OFF'", true);
    						if (turnOn)
    						{
    							debug("DrainAll will be turned ON");
    
    							// Following statement throws an exception:
    							// "Object reference not set to the instance of an object"
    
    							thisSorter.GetActionWithName("DrainAll_On").Apply(thisSorter);
    						}
    					}
    				}				
    			}
    		}
    	}		
    }
    
    void debug(string text, bool append = true)
    {
    	if (debug_panel == null) return; // No debug display found.
    	text += "\n";
    	debug_panel.WritePublicText(text, append);	
    	debug_panel.ShowTextureOnScreen();	
    	debug_panel.ShowPublicTextOnScreen();	
    }
    
     
    Last edited: Jan 26, 2018
Thread Status:
This last post in this thread was made more than 31 days old.