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

2014/9/10

some infinite loop is created..please help me i am new to greenfoot

rakesh8015 rakesh8015

2014/9/10

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** *if anyone requests sendgumball() * i need to send a random gumball which means i need to create gumball object on the screen * */ public class RandomPicker extends Picker { public void sendgumball() { int ball=1+Greenfoot.getRandomNumber(2); if(ball==1) { BlueGumball blueball=new BlueGumball(); getWorld().addObject(blueball,700,500); } if(ball==2) { GreenGumball greenball=new GreenGumball(); getWorld().addObject(greenball,700,500); } if(ball==3) { RedGumball redball=new RedGumball(); getWorld().addObject(redball,700,500); } } }
danpost danpost

2014/9/10

#
No infinite loop is apparent with the code provided. If you have one and this code is involved, then some other code is involved also -- like the gumball classes. Please show one of those (I will presume that all the gumball classes are basically the same code). It is also possible that the Picker class may be responsible. Maybe you should show the constructor of the Picker class, also.
rakesh8015 rakesh8015

2014/9/10

#
//i didn't use Picker nor Alien . both are empty and the alien class extends actor public class Picker extends Alien { public void act() { // Add your action code here. } } //from gumballmachine I am trying to call sendgumball public class GumballMachine extends Actor { public GumballMachine() { GreenfootImage image = getImage() ; image.scale( 350, 400 ) ; } RandomPicker randompick=new RandomPicker(); public void act() { randompick.sendgumball(); }
rakesh8015 rakesh8015

2014/9/10

#
Thank you very much ...you guys are doing a great job
rakesh8015 rakesh8015

2014/9/10

#
when i try to run the code i am getting exceptions java.lang.NullPointerException at RandomPicker.sendgumball(RandomPicker.java:30)-----(getWorld().addObject(blueball,700,500);) at GumballMachine.act(GumballMachine.java:64)-----randompick.sendgumball(); }
davmac davmac

2014/9/10

#
Please use code tags and fix your indentation when you post code. You are calling getWorld() in your RandomPicker class, but you are not adding the RandomPicker instance that you create (in GumballMachine) to the world. If you call getWorld() from an object that is not in the world, it will return null, leading to NullPointerException when you try to use the value.
rakesh8015 rakesh8015

2014/9/10

#
thank you ..:)
You need to login to post a reply.