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

2015/4/17

adding score error?

Jeph Jeph

2015/4/17

#
}
  private int fishEaten;
   private int score;
   
    public Happy()
    {
       fishEaten=0; 
       score=0;
    }

World myWorld = getWorld();
    public void eat()
    {
       if(canSee(Fish.class))
       {
          
           eat(Fish.class);
           
           fishEaten = fishEaten + 1;
           score = fishEaten;
           
         MyWorld jump =(MyWorld)myWorld;
         Counter counter =jump.getCounter();//error here
         counter.addScore();//
          if(fishEaten == 3)
           {
               Greenfoot.stop();
               NewWorld NewWorld = new NewWorld();
               Greenfoot.setWorld(NewWorld);
            }
           
           
        }
        
    }
Super_Hippo Super_Hippo

2015/4/17

#
Remove line 10 and change line 21 to this:
MyWorld jump = (MyWorld) getWorld();
davmac davmac

2015/4/17

#
Your line 10:
World myWorld = getWorld();
... is outside any method, so it runs when the object is created. At that point, the object is not in any world, so getWorld() returns null. Therefore you are effectively setting myWorld to null.
Jeph Jeph

2015/4/18

#
Thanks both ! its working now really appreciate it ,i understand my error now
You need to login to post a reply.