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

2014/12/30

HELP

largowinch123 largowinch123

2014/12/30

#
Hi, I need help. I am trying to create a deck image which contains all the cards of the deck. My cards name are an arrays of Strings which name is imageCards and are in the deck class. I have the world that create the deck. My problem is that the world doesn't create the deck. I don't know if i have explained well, so please tell me. ERROR MESSAGE: java.lang.NullPointerException DECK CLASS
public Deck() 
    {
        World world= getWorld();
        Card deck[] = new Card[40];
        int x = 0;
        for (int i=0;i<=deck.length;i++){
            
            deck[i] = new Card(imageCards[i]);
                world.addObject(deck[i],100+x,300);
                x = x + 6;
            
        }
    }
CARD CLASS
public Card(String i)
    {
       this.image= new GreenfootImage(i);
   
    }
WORLD public Mundo() { super(1000, 600, 1); this.addObject(new Deck(),100,300); }
Super_Hippo Super_Hippo

2014/12/30

#
You can't call 'getWorld()' in a constructor because the constructor is called before the object is added to the world. Well, you can call that method, but it will return 'null' and you can't call any method (in your case 'addObject') on 'null'.
You need to login to post a reply.