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 null reference exception for repeating inventory movement

Discussion in 'Programming Questions and Suggestions' started by VIEBlitz, Jan 4, 2015.

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

    Messages:
    17
    I've been trying to make a small script that will repeat on a timer block, moving inventory items back and forth between two crates. I know the script can move inventory items if I take out the part involving the antenna, but I want the script to check the name of the antenna in order to see which direction the items must move. Ever since I added in anything to do with antennas, I'm getting a null reference exception (but it compiles just fine) and I can't figure out where it's happening.
    Code:
    //a method for adding blocks to an existing list, based on type
    public void AddGroup<T>(List<IMyTerminalBlock> list)  
    {    
        var x = new List<IMyTerminalBlock>();
        GridTerminalSystem.GetBlocksOfType<T>(x); 
        list.AddRange(x);
    }
    //method that facilitates movement of items between inventories
    public void TransferAll(IMyInventory src, IMyInventory dest)   
    {   
        List<IMyInventoryItem> items = src.GetItems() as List<IMyInventoryItem>;   
        for ( int j = 1; j < items.Count; j++ )   
        {   
            if( items != null )  
            {  
                src.TransferItemTo(dest, 0, null, true, null);  
            }   
            else  
            {  
                break;  
            }  
        }   
    }
    void Main() 
    { 
        /*creates a list, adds the list of available antennas to it
        claims the first item of the list as a variable to be used*/
        List<IMyTerminalBlock> antennaList = new List<IMyTerminalBlock>();
        AddGroup<IMyRadioAntenna>(antennaList);
        IMyRadioAntenna antenna = antennaList[0] as IMyRadioAntenna;
    
    
        IMyInventoryOwner crate1 = GridTerminalSystem.GetBlockWithName("Crate") as IMyInventoryOwner;
        IMyInventoryOwner crate2 = GridTerminalSystem.GetBlockWithName("Crate 2") as IMyInventoryOwner; 
        IMyInventory inv1 = crate1.GetInventory(0) as IMyInventory; 
        IMyInventory inv2 = crate2.GetInventory(0) as IMyInventory;
        /*checks if antenna name is named according to first loop, or an even or odd cycle thereafter and moves items accordingly
        also renames the antenna so the next time the script is run, a different action will be performed*/
        if ( antenna.CustomName == "Debug Transfer" ) 
        { 
            antenna.SetCustomName("Flip"); 
            return; 
        }     
        else if ( antenna.CustomName == "Flip" ) 
        { 
            antenna.SetCustomName("Flop"); 
            TransferAll(inv1, inv2); 
            return; 
        } 
        else if ( antenna.CustomName == "Flop" ) 
        { 
            antenna.SetCustomName("Flip"); 
            TransferAll(inv2, inv1); 
            return; 
        } 
        else 
        { 
            return; 
        } 
    }
    
    If you recognize the first method in the code, it's because it's from a workshop cargo sorting script. The rest is my own coding though.
     
    Last edited by a moderator: Jan 4, 2015
  2. Cuber Apprentice Engineer

    Messages:
    262
    Well...have you checked if [FONT= &#39]antenna[/FONT] is null?
     
  3. VIEBlitz Trainee Engineer

    Messages:
    17
    Antenna is not null, I've got that for sure. I fixed some mismatches in block names (and took out that first method, replaced the way the script finds the antenna entirely) and now it's not throwing any errors at all but won't move the inventory items

    It currently looks like this:
    Code:
    <div>public void TransferAll( IMyInventory src, IMyInventory dest )
    {
        List&lt;IMyInventoryItem> items = src.GetItems() as List&lt;IMyInventoryItem>;
        for ( int j = 1; j &lt; items.Count; j++ )
        {
            src.TransferItemTo(dest, 0, null, true, null);
        }
    }
     
    void Main() 
    {
        List&lt;IMyTerminalBlock> antennaList = new List&lt;IMyTerminalBlock>();
        GridTerminalSystem.GetBlocksOfType&lt;IMyRadioAntenna>(antennaList);
        IMyRadioAntenna antenna = antennaList[0] as IMyRadioAntenna;    
        
        IMyInventoryOwner crate1 = GridTerminalSystem.GetBlockWithName("Crate") as IMyInventoryOwner; 
        IMyInventoryOwner crate2 = GridTerminalSystem.GetBlockWithName("Crate 2") as IMyInventoryOwner; 
        IMyInventory inv1 = crate1.GetInventory(0) as IMyInventory; 
        IMyInventory inv2 = crate2.GetInventory(0) as IMyInventory;
     
        if ( antenna.CustomName == "Flip" ) 
        { 
            antenna.SetCustomName("Flop"); 
            TransferAll(inv1, inv2);
        } 
        else if ( antenna.CustomName == "Flop" ) 
        { 
            antenna.SetCustomName("Flip"); 
            TransferAll(inv2, inv1);
        } 
        else 
        { 
            return; 
        } 
    }
    </div>
     
    Last edited by a moderator: Jan 4, 2015
  4. reptar1025 Trainee Engineer

    Messages:
    28
    Your TransferItemTo method isn't using the right arguments. The destination inventory ("dest" in your code) needs to be an IMyInventoryOwner not an IMyInventory. Also you have the item index for every call as "0", meaning it will only move the first item from the source inventory. You can check out my script's code on the workshop if you want to see how moving around items looks like: http://steamcommunity.com/sharedfiles/filedetails/?id=368050373. Hope I helped!
     
  5. VIEBlitz Trainee Engineer

    Messages:
    17
    The 0 is intentional, because every time I move an item out of slot 0, another one filters into that slot, and it continues until the items are all moved.

    So here's the weird thing: I had the same for loop in my TransferAll method working before I added in any code about any antennas, the only difference was that I used to have an if/else to check if the items in the inventory were null before I did the transfer.
    That all worked fine, then I tried to make the code check an antenna, and it all fell apart.

    I guess I'll try to make a new working item transfer script and then replace the dysfunctional one here with it.
     
    Last edited by a moderator: Jan 4, 2015
  6. reptar1025 Trainee Engineer

    Messages:
    28
    Disregard what I said about it having to be an IMyInventoryOwner, I was mistaken lol. So that looks fine but I also notice you start your loop at 1 (int j = 1) which means that if your src only had 1 item, the loop would be over before it started. Maybe that's why it wasn't working for you? Did you only have 1 item in the source inventory? You can fix it by either changing it to int j = 0 or j &lt;= items.Count.
     
    Last edited by a moderator: Jan 4, 2015
  7. VIEBlitz Trainee Engineer

    Messages:
    17
    D'oh. Been looking at the same code for so long I didn't even realize I started my loop at 1.
    That could be a problem, but I changed it to 0 and the items are still staying very much put.
    I had two items in each crate, was hoping the two items in the first crate would join the items in the second crate, and then on the next run of the script, all items would return to the first crate.
    Also just tried changing 0 in TransferItemTo to the variable j, to see if it would do anything differently or maybe even throw me an error message.
    No dice so far.

    Also, doing the loop in reverse order with
    Code:
    for ( int j = (items.Count - 1); j >= 0; j-- )
    
    and
    Code:
    src.TransferItemTo(dest, j, null, true, null);
    
    doesn't work either.
     
    Last edited by a moderator: Jan 4, 2015
  8. reptar1025 Trainee Engineer

    Messages:
    28
    I just had a thought. I found out earlier today that GetBlockWithName will get the first block containing the string you are asking for. Since you are looking for "Crate" and "Crate 2", maybe it is finding Crate 2 when you search for Crate, and Crate 2 again when you search for Crate 2. Maybe try changing the names to something that don't contain the same string to test that out? Like Source and Dest.
     
    Last edited by a moderator: Jan 4, 2015
  9. VIEBlitz Trainee Engineer

    Messages:
    17
    Oh thank goodness, you got it. I didn't think of that at all, but lo and behold, Space Engineers sorted the terminal and Crate 2 was above Crate. And now it's working perfectly!

    Thanks a bunch man
     
  10. reptar1025 Trainee Engineer

    Messages:
    28
    Np at all. Even though the programming guide says that GetBlockWithName gets the block with that exact name, it turns out that's not true :p.
     
  11. VIEBlitz Trainee Engineer

    Messages:
    17
    Yeah, good to know. I bet this will happen to me again. At least I'm prepared now :)
     
Thread Status:
This last post in this thread was made more than 31 days old.