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.

Getting Object instance not set to an instance of an object

Discussion in 'Programming Questions and Suggestions' started by Rusticza, Jan 20, 2018.

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

    Messages:
    4
    Hi Guys/ Gals,

    I am not really a programmer but I have dabbled with c over the last year or two, I am now trying to get my head around object orientated programming with C#.


    I am currently going through a tutorial series on youtube and thought I had a handle on the basics. I got visual studio setup for in-game scripting which has made my learning experience a lot easier, however, the script below seems fine in visual studio but will not run.

    It is just a test script that I am using to try and better understand what can be done in-game, it is not all my own creation.

    I would really appreciate any help here, it is probably something small, but for the life of me, i can't find it.

    Code:
    	const int kilowatts = 1000;
    		List<IMySolarPanel> panels = new List<IMySolarPanel>();
    		List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
    		List<IMyReactor> reactors = new List<IMyReactor>();
    		IMyTextPanel PowerPanel;
    		decimal PowerUsage = 0.0M;
    		bool currentUsageUpdated = false;
      
    		public Program()
    		{
    			GridTerminalSystem.GetBlocksOfType<IMySolarPanel>(panels);
    			GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);
    			GridTerminalSystem.GetBlocksOfType<IMyReactor>(reactors);
    			PowerPanel = GridTerminalSystem.GetBlockGroupWithName("PowerPanel") as IMyTextPanel;
    		}
    
    		public decimal findCurrentUsage()
    		{
    			decimal answer = 0.0M;
    			for (int i = 0; i < panels.Count; i++)
    			{
    				answer += (Math.Round(Convert.ToDecimal(kilowatts * panels[i].CurrentOutput),3));
    			}
    			for (int i = 0; i < batteries.Count; i++)
    			{
    				answer += (Math.Round(Convert.ToDecimal(kilowatts * batteries[i].CurrentOutput),3));
    			}
    			for (int i = 0; i < reactors.Count; i++)
    			{
    				answer += (Math.Round(Convert.ToDecimal(kilowatts * reactors[i].CurrentOutput), 3));
    			}
    			currentUsageUpdated = true;
    			return answer;
    		}
    
    		public void displayToPanels()
    		{
    			if (currentUsageUpdated)
    			{
    				PowerPanel.WritePublicText("Current Power Usage :" + PowerUsage);
    			}
    	//space for future panels and info
    		}
    		void Debug(string text, bool append)
    		{
    			IMyTextPanel debugScreen;
    			debugScreen = GridTerminalSystem.GetBlockGroupWithName("DEBUG") as IMyTextPanel;
    			debugScreen.WritePublicText(text + "\n", append);
    	//incomplete, will implement later.
    		}
    
    		public void Main(string argument)
    		{
    			PowerUsage = findCurrentUsage();
    			displayToPanels();
    		}
     
    Last edited: Jan 20, 2018
  2. Wicorel Senior Engineer

    Messages:
    1,242
    As noted on the discord, in the Debug() function you are getting a block group, but assigning it to a single text panel.
     
  3. Rusticza Trainee Engineer

    Messages:
    4
    thanks for the reply Wicorel,

    I have fixed the Debug function to GetBblockWithName. However, the error is still preset If I comment out the PowerUsage = FindCurrentUsage() in Main() the run time error disappears. I had some suggestion earlier on the discord server that the IMytextPanel might be returning a null value. I have checked and the ownership and panel name is correct. Will keep tinkering with it. Thanks again for taking the time to respond.

    Edit: /me face palms

    Found it thanks, Wicorel, intellisense is making me lazy
     
    Last edited: Jan 20, 2018
Thread Status:
This last post in this thread was made more than 31 days old.