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

2015/12/2

Unkown Errors and Exceptions with Integer.parseint()

polypoly polypoly

2015/12/2

#
Hey I'm having some issues with this bit of code in my world constructor. My code looks like this:
public class MyWorld extends World
{
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 100, 1, false); 
        
        //prompts the user for how far away to place a Treasure object.
        String str = Greenfoot.ask("how far away, in cm is the treasure?");
        int tDist = Integer.parseInt(str.trim())+5;
        addObject(new Treasure(), getWidth()/2 + tDist, 50);
        //creates a starting line and a robot
        addObject(new Start(), getWidth()/2, 50);
        addObject(new Robot(), getWidth()/2, 50);
    }
and yet when it compiles it says "The world constructor threw an exception." this is the exception:
java.lang.NullPointerException
	at MyWorld.<init>(MyWorld.java:22)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at greenfoot.core.Simulation.newInstance(Simulation.java:607)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:445)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494)
	at greenfoot.core.Simulation.maybePause(Simulation.java:299)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
whats going on? I'm very new to java and greenfoot
SteveBarnes SteveBarnes

2015/12/2

#
It looks like str isn't being assigned a value, so it contains null try String str = Greenfoot.ask("how far away, in cm is the treasure?"); if( str != null ) { int tDist = Integer.parseInt(str.trim())+5; addObject(new Treasure(), getWidth()/2 + tDist, 50); //creates a starting line and a robot addObject(new Start(), getWidth()/2, 50); addObject(new Robot(), getWidth()/2, 50); } else { // str was null, so display an error System.out.println("str was null\n"); } it's always a good idea to make sure any data you read in from a user or a file actually exists before you try to use it :)
Super_Hippo Super_Hippo

2015/12/2

#
If you don't type anything into the box, 'str' will be "", not 'null'. I am not sure what is going on there, but maybe it could be helpful if you tell us which line is line 22 in the code.
danpost danpost

2015/12/2

#
Super_Hippo wrote...
If you don't type anything into the box, 'str' will be "", not 'null'.
@Super_Hippo, I am not working with a late enough version of greenfoot to be able to test the 'ask' method. Is it possible to close the frame (clicking at top-right corner) and have a 'null' value returned? @Greenfoot team, the 'ask' method does not have 'Parameters' and 'Return' segments in the API documentation.
Super_Hippo Super_Hippo

2015/12/2

#
Ok, I found the problem I think. If you change the code and click 'compile', it should work. But if you just open the scenario, the 'ask' method doesn't seem to work. I just tested it with these code:
import greenfoot.*;
public class MyWorld extends World
{
    public MyWorld()
    {
        super(600, 400, 1); 
        String a = Greenfoot.ask("What is your name?");
        System.out.println(a);
    }
}
So if I close Greenfoot and re-open it, it prints 'null'. If I am inside Greenfoot and compile (which is only possible when changing something in the code (so I add one letter and undo it again, then click compile)), then the message shows up and the input is printed afterwards. @danpost: It looks like the following, there isn't a frame to click an 'X' or something to close it. Only an 'OK' button. Screenshot
polypoly polypoly

2015/12/3

#
@SteveBarnes works perfectly and makes sense. because before the user inputs the ask its trying to make it into an integer for tDist. i guess parseInt() doesn't have a way of handling null values? either way, thanks for the help
You need to login to post a reply.