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

2012/6/26

I got an erro and don't know why

Mux Mux

2012/6/26

#
The error is: java.lang.NullPointerException at Zentrale.OpenMenu(Zentrale.java:275) I get a idea behind it that may be a littel bit confusing but anyway the code works like this: if the button is clicked it calls the "Zentrale" Object, that isn't implemented in the world, over the "Spiel" class, that is a subclass of World, to do the method: OpenMenu(String). The String is used to know wich Menu should open.
public void OpenMenu(String Menu)
    {
        if( Menu == "UpperMenu")
        {
            if(UpperMenu.isEmpty()==true)            // List isn't set up.
            {
                Button Close = new Button("UpperMenu",2);
                Button Option  = new Button("UpperMenu",3);
                
                UpperMenu.add(0,Schließen);
                UpperMenu.add(1,Optionen);
                
            }
            Spiel w = (Spiel)getWorld();
            w.Insert(UpperMenu);
        }
    }
Insert() inserts the Objects out of the "UpperMenu" list into the world. I want to do it like this, becaus it is quite nice to get everything listed up and just shar this list to creat a big amount of objects. This might be an more complicatet way first, but if you have to fill in more menus it is easier to have everything at a spot and listed up. The problem is the error message tells me to look in line 275 and this line is:
w.Insert(UpperMenu);
and i can't figure out why it doesn't work. Here the Insert() code:
public void Insert(java.util.ArrayList List)
    {
        for(int n = 0; n <= List.size(); n++)
        {
            if( List.get(n) != null)
            {
                
                if(List.get(n) == MenuWindow.class)
                {
                    MenuWindow M = (MenuWindow)List.get(n); 
                    addObject(M,M.GiveX(),M.GiveY());
                }
                
                if(List.get(n) == Button.class)
                {
                    Button M = (Button)List.get(n);  
                    addObject(M,M.GiveX(),M.GiveY());
                }

            }
        }
    }
Just hope i don't confuse you too much and somebody is able to give me an answer ;). I want to know where the mistake is.
Mux Mux

2012/6/26

#
The main problem is: I get a list of many objects every object with x_parameter and y_parameter. The world shoud have a method that allows to insert all object out of the list into the world. The x and y position is get from every object itself. If there is a better way to get a list of objects into the world let me know it pls.
davmac davmac

2012/6/26

#
The problem is probably that 'w' is null, because getWorld() returns null. This happens when an actor isn't in the world (yet). Probably you're calling OpenMenu from the constructor? By the way, convention is to name methods beginning with a lower case character: openMenu instead of OpenMenu!
Mux Mux

2012/6/26

#
Oh dear ... I have a methode to give the world to the "Zentrale" becaus it's not in the world and than i use the getWorld(). wow big big mistake ^^ Thanks and by the way i'll get all methods starting with a lower case character, great tipp.
kiarocks kiarocks

2012/6/26

#
Also, you should name your variables like 'm' and 'close' instead of 'M' and 'Close'.
Mux Mux

2012/6/27

#
Ok. I'll fix this too.
You need to login to post a reply.