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

2018/4/11

Greenfoot.ask to int in constructor

TheGoldenProof TheGoldenProof

2018/4/11

#
I have a program that calculates a value based on user input, and currently I have the greenfoot.ask in the constructor. everything works fine if i drop the calculator class into the world, but if I try to save the world, it will not compile and i get java.lang.NumberFormatException: null. Here is the code constructor code for the calculator (this isn't all of the calculator code just what relates to the constructor):
1
2
3
4
5
6
7
8
int startingNumber = 1;
List<Integer> steps = new ArrayList<Integer>();
 
public Calculator() {
        steps.add(Integer.parseInt(Greenfoot.ask("enter a number >0 (preferably <1000 too and dont do anything rediculous like 9999....)")));
        GreenfootImage startingNumber = new GreenfootImage("Starting Number: " + steps.get(0), 40, Color.WHITE, Color.BLACK);
        setImage(startingNumber);
    }
I have tried a few things like assigning greenfoot.ask to a variable and the parsing that variable and i got the same result.
danpost danpost

2018/4/11

#
You are probably creating the calculator during project reset (when world is first instantiated and before the scenario is in a running state). Try to avoid using ask at that time.
TheGoldenProof TheGoldenProof

2018/4/12

#
danpost wrote...
You are probably creating the calculator during project reset (when world is first instantiated and before the scenario is in a running state). Try to avoid using ask at that time.
Thanks, I figured it out. I changed it to check whether or not a value had been entered or not in the act.
You need to login to post a reply.