This site requires JavaScript, please enable it in your browser!
Greenfoot back
kiarocks
kiarocks wrote ...

2011/12/25

Why null pointer?

1
2
kiarocks kiarocks

2011/12/25

#
I get a null pointer pointing to this code:
cards.add(card);
I provide the method it is in (addCard(Card card)) with "this", so it shouldn't be null.
danpost danpost

2011/12/26

#
The keyword 'this' would be a Card? Where, or what class is this particular addCards(Card card) method in, and where is it being called from? NOTE: that operation returns a boolean value; try
boolean dummy = cards.add(card);
kiarocks kiarocks

2011/12/26

#
No, it is a void method and this is In card.class
kiarocks kiarocks

2011/12/26

#
public void addCard(Card card)  
    {  
        cards.add(card);
        lastCardSpot = cards.lastIndexOf(card);
        topCard = cards.get(lastCardSpot);
    }  
(Pad.class)
kiarocks kiarocks

2011/12/26

#
Called from card
kiarocks kiarocks

2011/12/26

#
And it worked before without any problems.
Builderboy2005 Builderboy2005

2011/12/26

#
card may not be null, but have you checked to make sure cards is not null?
kiarocks kiarocks

2011/12/26

#
Note which line it points to in the first post.
Builderboy2005 Builderboy2005

2011/12/26

#
I'm not sure what you mean? In no parts of the code you have posted do you show where you initialize cards, is it possible it is null?
kiarocks kiarocks

2011/12/27

#
The method is called from a card in the world by right click.
Builderboy2005 Builderboy2005

2011/12/27

#
Why does that have anything to do with the object cards (note the plural) being null or not? Where is cards initialized?
kiarocks kiarocks

2011/12/27

#
the world:
public void addRow(int rownum)
    {
        for(int i = 1; i != 2; i++)
        {
            setVars();
            if(!taken.takenCardNumAndSuit[randnum][suit])
            {
                addObject(new Card(true,randnum,suitS,false),135+(80*rownum),165+(20*rownum));
                taken.takenCardNumAndSuit[randnum][suit] = true;
            }
            else
            {
                if(!taken.takenCardNumAndSuit[randnum][suit])
                {
                    setVars();
                    addObject(new Card(true,randnum,suitS,false),135+(80*rownum),165+(20*rownum));
                    taken.takenCardNumAndSuit[randnum][suit] = true;
                }
                else
                {
                    i--;
                }
            }
        }
        for(int i = 1; i != (7-rownum); i++)
        {
            setVars();
            if(!taken.takenCardNumAndSuit[randnum][suit])
            {
                addObject(new Card(false,randnum,suitS,false),135+((i+rownum)*80),165+(20*rownum));
                taken.takenCardNumAndSuit[randnum][suit] = true;
            }
            else
            {
                setVars();
                if(!taken.takenCardNumAndSuit[randnum][suit])
                {
                    addObject(new Card(false,randnum,suitS,false),135+((i+rownum)*80),165+(20*rownum));
                    taken.takenCardNumAndSuit[randnum][suit] = true;
                }
                else
                {
                    i--;
                }
            }
        }

    }
called in constructor. every thing is fine until a card runs act method.
public void act() 
    {
        checkDrag();
        if(!dragging && pad != null)
        {
            pad.addCard(this);
        }
    }  
Builderboy2005 Builderboy2005

2011/12/27

#
Nothing in the code you posted references cards. I ask again, where is the object cards initialized?
kiarocks kiarocks

2011/12/27

#
Oh, in Pad.class:
List cards;
kiarocks kiarocks

2011/12/27

#
So yes, cards is null, but how does that change anything?
There are more replies on the next page.
1
2