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

2020/5/29

Act method causing error in switch statement (getKey())

1
2
3
4
444Jam444 444Jam444

2020/5/29

#
https://www.greenfoot.org/scenarios/25669 See comments: "Known issue" This is to be used for switching worlds. I have no problem switching from the main world to the mode selection world, but switching back is an issue. I have put in try... catch to handle the specific error I was getting, but that seems to have made it worse; it gets stuck mid-world-change without giving an error message (even though Catch is supposed to print a message in the terminal) I am having an issue with the act method of a certain class. I want to register a single key by looping through getKey until getKey is not null, then execute a switch statement according to key = getKey (if getKey != null), switch (key). key == null even though I have put in measures to stop this. NullPointerException: switch (key) I printed the value for key, and for some reason it was null, even after making sure that key should not be null.
public class ModeChanger extends Actor
{
    private String key = "";
    /**
     * Act - do whatever the ModeChanger wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if (Greenfoot.getKey() != null){
            key = Greenfoot.getKey();
            if (key != null){
                change();
                return;
            }   
        }
    }    
    private void change(){
        System.out.print(key);
        try{
            switch (key){
                case "1":
                    Greenfoot.setWorld(new Calculator());
                    return;
                case "2":
                    Greenfoot.setWorld(new ShowCalcHistory());
                    return;
                case "3":
                    Greenfoot.setWorld(new ShowCalcHistory("steps"));
                    return;
                case "4":
                    Greenfoot.setWorld(new ViewPronumerals());
                    return;
                default:
                    break;
            }
        }
        catch(RuntimeException error){
            System.out.println("The interface you selected could not initialise in time. Please restart the program.");
            error.printStackTrace();
        }
    }
}
Super_Hippo Super_Hippo

2020/5/29

#
“Greenfoot.getKey” reads and deletes a variable, the last pressed key. Using the method twice in one act cycle won’t work.
danpost danpost

2020/5/29

#
Remove lines 11 and 17. Then remove line 21 and lines 38 thru 42. If you have issues going to a new world, show the class codes of the attempted world.
danpost danpost

2020/5/31

#
danpost wrote...
Remove lines 11 and 17. Then remove line 21 and lines 38 thru 42. If you have issues going to a new world, show the class codes of the attempted world.
Nudge.
4444Jam4444 4444Jam4444

2020/5/31

#
Hi danpost, I have just made an alt account specifically to raise concerns about this issue. I responded to your comment and it seems that, by posting my world code, my comment was auto-marked as spam. I posted this on the scenario that I uploaded for this project (doubt it'll be seen by an admin any time soon so I may as well post it here):
444Jam444 wrote...
I was asked to post my world code by danpost (even though I had stated that I had posted my scenario and included the link to the scenario) and my comment was auto-marked as spam. It contained zero spam or offensive language. I am now not allowed to continue to ask for help on my own discussion for the duration of the review period. It's been 2 days. The review period is meant to be 24h. This project is due in less than 2 weeks and I don't have time for this. Please help.
4444Jam4444 4444Jam4444

2020/5/31

#
My original comment was this:
444Jam444 wrote...
Super_Hippo wrote...
“Greenfoot.getKey” reads and deletes a variable, the last pressed key. Using the method twice in one act cycle won’t work.
I'm not sure what you mean. I kinda understand where you're going with this...? Originally I had
key = Greenfoot.getKey();
if (key != null){
    switch (key){
        etc.
        etc.
    }
}
but that didn't work, produced nullpointerexception. I've tried using break in each switch case, tried return in each switch case (does return break the act() method? If so, that would help me here I think)
danpost wrote...
If you have issues going to a new world, show the class codes of the attempted world.
I included the link to the WIP scenario. But, because you asked (it's unfinished, hence the many comments); (Code omitted because my comment was marked as spam for posting my world code!!! PLEASE just visit the scenario! https://www.greenfoot.org/scenarios/25669)
4444Jam4444 4444Jam4444

2020/5/31

#
This has been a bit frustrating, not going to lie. Hopefully this all gets resolved and doesn't happen again. I don't really care all that much about what happens to this alt account, but I'll only use it for stuff like this specific situation. I'll probably forget it exists somewhere down the line.
4444Jam4444 4444Jam4444

2020/5/31

#
Oh and another thing I should mention, the CAPTCHAA for creating a new account is completely broken. The image is all glitched out, warped and impossible to read. Had to refresh the page multiple times to get one that was only slightly broken. Definitely an issue.
danpost danpost

2020/5/31

#
Your CheckKeyPress object in your world is grabbing any key input before your ModeChanger object can acquire it. In short, getKey will only work properly ONCE in any act cycle. For example:
String key = Greenfoot.getKey();
String key2 = Greenfoot.getKey();
if (key != null) System.out.println(""+(key.equals(key2)));
will pretty much always print "false". In fact, key2 will almost always be null. It will not matter that the second call is in another class or not. If called during the same act cycle, the behavior is the same.
4444Jam4444 4444Jam4444

2020/5/31

#
Hold on a moment, let me try that. Also, may I ask the situation on reviewing my comment?
danpost danpost

2020/5/31

#
4444Jam4444 wrote...
Also, may I ask the situation on reviewing my comment?
Please elaborate. (what, or which, comment?)
4444Jam4444 4444Jam4444

2020/5/31

#
Ok, that fixed it. For some reason, I was apparently trying to stop key from being registered as null before even trying the simple way. Another issue that I'm having is with scrolling. I have copied (without modification, yet) this post: https://www.greenfoot.org/topics/155 getWorldCanvas() is not working. How do I work around this?
WorldHandler.getInstance().getWorldCanvas().addMouseWheelListener(scroll);
Cannot find symbol - method getWorldCanvas
4444Jam4444 4444Jam4444

2020/5/31

#
4444Jam4444 wrote...
My original comment was this:
444Jam444 wrote...
Super_Hippo wrote...
“Greenfoot.getKey” reads and deletes a variable, the last pressed key. Using the method twice in one act cycle won’t work.
I'm not sure what you mean. I kinda understand where you're going with this...? Originally I had
key = Greenfoot.getKey();
if (key != null){
    switch (key){
        etc.
        etc.
    }
}
but that didn't work, produced nullpointerexception. I've tried using break in each switch case, tried return in each switch case (does return break the act() method? If so, that would help me here I think)
danpost wrote...
If you have issues going to a new world, show the class codes of the attempted world.
I included the link to the WIP scenario. But, because you asked (it's unfinished, hence the many comments); (Code omitted because my comment was marked as spam for posting my world code!!! PLEASE just visit the scenario! https://www.greenfoot.org/scenarios/25669)
4444Jam4444 4444Jam4444

2020/5/31

#
The comment hasn't been posted because, after 2 days, it is still pending review. The only difference between the original comment and the above quote is that I didn't include my world code. I assume it's because of the length of the code and the sheer number of /** */ comments
danpost danpost

2020/5/31

#
The getWorldCanvas method only worked in the Legacy versions of greenfoot. I am not privy to any workarounds.
There are more replies on the next page.
1
2
3
4