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

2013/11/1

nullPointer with removeObject :S

JimmyFake JimmyFake

2013/11/1

#
Hi Greenfoot-Community, I have a little problem. As the topic says a null pointer exception is triggered, but I can't comprehend why. Here is the important part:
private void replacing()
    {
        java.util.List<Actor> actors = getObjects(Actor.class);
        for(Actor a: actors)
        {
            if(!(a instanceof Player))
            {
                if(a.getX() >= getWidth() -30)
                {
                    worldMap.put((worldMapX + a.getX()) + "#" + (worldMapY + a.getY()),a);
                    System.out.println(a);
                    removeObject(a); 
                }else 
                {
                    a.setLocation(a.getX()+worldX, a.getY()+worldY);
                }
            }
        }
    }
replacing() is called in the act() method of the world. Everything except the player has to be moved, according to the worldX/Y coordinates. The method isn't finished yet, but as for now, it was working without exceptions,until i put the removeObject part into it. I already used the debugger to see, whether a contains an actor when it enters the if-clause and it does. So why does it throw a nullpointer exception at removeObject? Thanks for help Sincerely, Jimmy
danpost danpost

2013/11/1

#
The only thing I could think might be causing this is the 'put' call on 'worldMap'. Please explain what 'worldMap' is and show the code for 'put'.
bourne bourne

2013/11/1

#
I really don't see how a NullPointerException could come from removeObject, especially when "a" is not set null locally ->so it's not null. Are you removing Actors (which appear later in the list "actors") from the World somewhere along the call chain of the put method call that produces the exception actually when accessing the x-coordinate of "future" a's?
Oxy Oxy

2013/11/2

#
"a" is not set to null at any point, but the object in which it is pointing can be null. ( For example, no actors in Actor list) Can you please post the error message?
bourne bourne

2013/11/2

#
Oxy wrote...
"a" is not set to null at any point, but the object in which it is pointing can be null. ( For example, no actors in Actor list) Can you please post the error message?
Considering getObjects will never give you a list containing null values, No, not in Java (different from pointers in C). Unless there is something about Object's finalize method I'm not familiar with. And the variable "a" never comes into being if the list is empty
danpost danpost

2013/11/2

#
Oxy wrote...
"a" is not set to null at any point, but the object in which it is pointing can be null. ( For example, no actors in Actor list)
I am afraid this cannot be the case. All actors in the world will be listed and the 'for' loop will iterate through for each one listed (if no actors are listed, the 'for' loop will execute exactly zero times). If the actor listed is a Player instance, no action is taken; otherwise, if the object is near the right edge of the world,'put' will be executed on the 'worldMap' object, the system terminal will display some data and the object is removed from the world, and if it is not near the right edge of the world, it is shifted within the world by an offset of (worldX, worldY).
JimmyFake JimmyFake

2013/11/2

#
danpost wrote...
The only thing I could think might be causing this is the 'put' call on 'worldMap'. Please explain what 'worldMap' is and show the code for 'put'.
worldMap is a java.util.Map, declared like this:
Map<String, Actor> worldMap;
the put method is not implemented by me, just looked it up here http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#put(K, V)
bourne wrote...
I really don't see how a NullPointerException could come from removeObject, especially when "a" is not set null locally ->so it's not null. Are you removing Actors (which appear later in the list "actors") from the World somewhere along the call chain of the put method call that produces the exception actually when accessing the x-coordinate of "future" a's?
I don't think so, the put method only returns the previous Actor that was mapped with the same key, or null if there wasn't any. But I didn't write anything like
a = worldMap.put((worldMapX + a.getX()) + "#" + (worldMapY + a.getY()),a);
danpost danpost

2013/11/2

#
Try replacing lines 3 through 5 with the following (I do not know if it will help, but it would not hurt to try)
for (Object obj : getObjects(null))
{
    Actor a = (Actor) obj;
    if (!(a instanceof Player))
    {
        // etc.
JimmyFake JimmyFake

2013/11/3

#
danpost wrote...
Try replacing lines 3 through 5 with the following (I do not know if it will help, but it would not hurt to try)
for (Object obj : getObjects(null))
{
    Actor a = (Actor) obj;
    if (!(a instanceof Player))
    {
        // etc.
Unfortunately, this doesn't work. Still the same error, nullpointer.
danpost danpost

2013/11/3

#
Do you get the same result when you remove the 'System.out.println' line?
JimmyFake JimmyFake

2013/11/3

#
danpost wrote...
Do you get the same result when you remove the 'System.out.println' line?
Yes, same result. Actually I just put in the System.out.println to test whether a is really null at the moment. Strangely enough, the console does't even print out a before the nullpointer shows up. That's how the errorcode looks like with and without the System.out.println:
java.lang.NullPointerException
	at Level1.replacing(Level1.java:68)
	at Level1.act(Level1.java:105)
	at greenfoot.core.Simulation.actWorld(Simulation.java:574)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:509)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
And the console output screen ist always clean (no a is printed).
danpost danpost

2013/11/3

#
Try inserting before the first 'if' the following and report back the results:
System.out.println("world map exists: "+(worldMap != null));
Zamoht Zamoht

2013/11/3

#
You never create a new Map object for your worldMap reference it seems.
Zamoht Zamoht

2013/11/3

#
I found this on a stackoverflow thread. Map is an interface. Create an instance of one the classes that implements it: Map<String, String> temp = new HashMap<String, String>(); temp.put(colName, data); Or, in Java 7: Map<String, String> temp = new HashMap<>(); temp.put(colName, data);
JimmyFake JimmyFake

2013/11/3

#
Zamoht wrote...
I found this on a stackoverflow thread. Map is an interface. Create an instance of one the classes that implements it: Map<String, String> temp = new HashMap<String, String>(); temp.put(colName, data); Or, in Java 7: Map<String, String> temp = new HashMap<>(); temp.put(colName, data);
That's it. I overlooked the fact that Map is an inferface. Initializing it with the HashMap Implementation, lifts the nullpointer exception.
danpost wrote...
Try inserting before the first 'if' the following and report back the results:
System.out.println("world map exists: "+(worldMap != null));
I guess I don't have to report back the results then ^^ Thanks everyone for the help :) Even though the problem's been solved, maybe someone could give me a tip, which Implementation of Map is the most efficient to use, or whether maybe something else than map would be even better. I want to store Actors inside the map(HashMap, Array, whatever), together with an x and y coordinate of this Actor. I don't think that putting the to coordinates together should do any trouble in my code, but if there is a nice method to store two int's as a key.
You need to login to post a reply.