0

I have recently been giving Java spending a good few months on C#, just got caught up on something I'm unsure how to format.

A little example I've thrown together to explain my problem.

I am trying to add a an object of type 'Creature' to my object of type 'Player'

Currently like this:

                                                        //Null items are objects
Player newPlayer = new Player("This", "Is", "Here", 1 , 1, 1, null, null, null);        

Creature c = null;                                  //Null items are objects
c = new Creature("Name", "Species",  100, 5.5, 10.5, 1, 100, null, null, null);

newPlayer.addCreature(c);

However the problem I'm getting is java.lang.NullPointException.

The player class can be seen here:

public Player(String Username, String Password, String Email, int Tokens, int Level, int Experience, Vector<Creature> Creature, Vector<Food> Food, Vector<Item> Item) {
    m_username = Username;
    m_password = Password;
    m_email = Email;
    m_tokens = Tokens;
    m_level = Level;
    m_experience = Experience;
    m_creature = Creature;
    m_food = Food;
    m_item = Item;

}   

public void addCreature(Creature c)
{
    m_creature.add(c);      
}

And the creature:

public Creature(String Name, String Species, int Health, double Damage, double Regen, int Level, int Exp, Vector<Effect> ActiveEffect, Vector<Attack> Attack, Vector<Specialisation> Specialisation )
{
    m_name = Name;
    m_species = Species;
    m_health = Health;
    m_damageMultiplier = Damage;
    m_regenRate = Regen;
    m_level = Level;
    m_experience = Exp;
    m_activeEffect = ActiveEffect;      
    m_attack = Attack;
    m_specialisation = Specialisation;      

}

How do I create the instance using this?

3
  • 2
    can you write the exception log? Commented Dec 9, 2012 at 13:31
  • whats your class definition for Player and Creature? Do you have a stack trace? A shot in the dark: did you instantiate a collection (new ArrayList(), etc...) object in your Player object, or did you only define it? Commented Dec 9, 2012 at 13:31
  • Could you please post the code of Player? Is Creature added to a list inside player, or just into a member variable? If it is added to a list, the list may be null! Commented Dec 9, 2012 at 13:40

2 Answers 2

1

That is because the reference to the vector that you are storing is null. You are passing nulls for the constructor.

When you pass new vector<Creature>() you are actually passing a reference to a newly constructed vector. It doesn't contain any creature objects as yet. Earlier it was failing because you were trying to call add(..) function on a reference that was set to null.

Try this:

Player newPlayer = new Player("This", "Is", "Here", 1 , 1, 1, new Vector<Creature>(), new Vector<Food>(), new Vector<Item>());
                                                              ^ new empty vector      ^ new empty vector  ^ new empty vector
Sign up to request clarification or add additional context in comments.

Comments

0

It is impossible to say without looking at the addCreature implementation. Look carefully the Stackstrace of the exception, it will show you the exact line number where the exception happened.

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.