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

2012/6/17

null pointed exception error

wahaj wahaj

2012/6/17

#
EDIT: I misspelled the title. I'm getting the following error java.lang.NullPointerException at The_World.mouse(The_World.java:86) at The_World.questions(The_World.java:38) at The_World.act(The_World.java:28) at greenfoot.core.Simulation.actWorld(Simulation.java:513) at greenfoot.core.Simulation.runOneLoop(Simulation.java:456) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) here is the code I'm using
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class The_World here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class The_World extends World
{
    public static int counter = 0;
    int mousex;
    int mousey;

    /**
     * Constructor for objects of class The_World.
     * 
     */
    public The_World()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1); 

    }

    public void act()
    {
        questions();
    }

    public void questions()
    {
        if (counter == -1)
        {   clearWorld();

            addObject(new Text("you lose"), 400, 400 );

            mouse();
            if (mousex >0 && mousey >0) 
            {
                counter = 0;
            }
        }

        if (counter == 0)
        {
            clearWorld();
            addObject(new Text("Click Circle"), 300, 50 );
            addObject(new Shape(1,2,50,50,102,0,0,255 ), 109,402); //answer
            addObject(new Shape(0,2,70,50,102,0,153,255),119,232);
            addObject(new Shape(0,1,50,50,204,0,105,255),456,202);
            addObject(new Shape(0,1,70,50,51,0,205,255),433,400);
            if (Greenfoot.mouseClicked(null) )
            {
                mouse();
                if ( (mousex > 59 && mousex < 159)&& (mousey > 350 && mousey < 452) )    
                {  
                    counter = 1;
                }  
                else 
                { 
                    counter = -1;
                }
            }


        }
        if (counter == 1 )
        {
            clearWorld();
        }
    }

    public void clearWorld()
    {
        removeObjects(getObjects(null) );
    }

    public  void  mouse()
    {

        {

            MouseInfo mouse = Greenfoot.getMouseInfo();

            mousex = mouse.getX();
            mousey = mouse.getY();

        }
    }

}
Please help. I've been at this for a long time and I just cant seem to figure things out. I fix one problem and then 5 minutes later another problem comes up. its really frustrating. in my code where I'm getting the error all I'm trying to do is see if the mouse is clicked inside the specified rectangle. if it is set the value of the counter to 1 and if not then set the value to -1. 1 leads to the next question while -1 ends the game.
danpost danpost

2012/6/17

#
Remove lines 38 through 42 (or replace them with 'Greenfoot.stop(); return;'). You did specify that if counter was -1 that the game would end. And, it was the calling of 'mouse();' here that caused the NullPointerException to be thrown.
wahaj wahaj

2012/6/17

#
well thanks. but I kinda figured it out before you answered. and I'm pretty sure I wont be asking anymore questions because my game is almost finished. at this point its just a matter of how fast I can type. thanks for all your help. really appreciate it.
You need to login to post a reply.