0

I'm trying to write an inventory system for an RPG. I have custom type called 'Item', and a class called 'Bag'. An item can be put into a bag with this function:

public Item aw = new Item();
public void PutIn(Item aw)
{
    Contents[Index]=aw;
    Index++;        
}

When I call the function with

Bag.PutIn(someItem);

in the main code it throws a System.NullReferenceException. By researching the issue I found out that this is usually caused by improperly initialised objects, but in this case I did initialise the object with Item aw = new Item();. What am I doing wrong?

2
  • 5
    Contents[Index]=aw; is throwing the System.NullReferenceException. How do you initialize Contents? Commented Mar 19, 2014 at 8:24
  • 3
    We need to see more of your code, what you've included is not enough. Commented Mar 19, 2014 at 8:26

1 Answer 1

1

Its not the Item object that is not initialized. You probably didn't initialize "Contents" array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.