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

2014/10/15

getting nullpointer exception

ngokhale ngokhale

2014/10/15

#
public class smiley extends Actor {//MouseInfo m=new MouseInfo(); /** * Act - do whatever the smiley wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ Counter a; //int c1; public smiley(Counter c) {//c1=0; a=c; } public void act() { //setImage("smiley1.png"); //// move(1); move(1); if(isAtEdge()) {// c1++; a.add(1); if(a.getValue()>=2) { setImage("button-green.png"); } setImage("button-blue.png"); turn(Greenfoot.getRandomNumber(10)); } } }
danpost danpost

2014/10/15

#
Need to see the class code where you create the smiley and counter and add them into the world (including field declarations).
ngokhale ngokhale

2014/10/15

#
public class ground extends World { Counter c; /** * Constructor for objects of class ground. * */ public ground() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); for(int i=1;i<=10;i++) { // if(Greenfoot.getRandomNumber(100)<5) { int x = Greenfoot.getRandomNumber( getWidth()-10 ); int y = Greenfoot.getRandomNumber( getHeight()-5 ); addObject(new smiley(c),x,y); } prepare(); } public void act() { } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Counter counter = new Counter(); addObject(counter, 524, 375); } }
Super_Hippo Super_Hippo

2014/10/15

#
You have 'Counter c' in your world class, but this is not the counter you create in the prepare method. So the object 'c' which you pass to the 'smiley' class is 'null'. What you could do is, change the 'prepare' method to the following and call in the constructor before you create the smileys, so right after the 'super(...);'
ngokhale ngokhale

2014/10/16

#
Thank u. My problem get solved.
ngokhale ngokhale

2014/10/16

#
An application has to e designed using 10 blue balls. When these balls hit the edge color changes into yellow and when it touches the edge second time color changes to red. How can i keep track of count of edge touching. When it touches for first time color is changing into yellow but i am not able to make it red. pls suggest solution.
Super_Hippo Super_Hippo

2014/10/16

#
Oh sorry, I said you should change the method to the following but because I failed with the ending 'code' tag, it wasn't visible...
    private void prepare()
    {
        c = new Counter();
        addObject(c, 524, 375);
    }
For your current problem, you can have a counter. So you can write, if the ball hits the edge, the counter is increased by one. If the counter is 1 now, change color to yellow, else you change it to red. When the ball hits the edge and the method that you use doesn't transport the ball to a region which is not detected as the edge, then you need to create a boolean, too. So you only increase the counter if it just weren't at the edge and now it is.
You need to login to post a reply.