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

2014/12/13

NullPointerException with getObjectAtOffset

ds06 ds06

2014/12/13

#
Hello, following code throws me a NullPointException and i cant figure out why:
1
2
3
4
5
6
7
8
9
10
public void checkMouse(){
        if(Greenfoot.mouseClicked(null) && !gotTarget){
             
            Actor f = getOneObjectAtOffset(300,300,Gegner1.class);
            getWorld().addObject(new Spell3(), f.getX(), f.getY());
            gotTarget=true;
 
         
    }
}
It should and an Obect at the Location of an Gegner1.class Object, but it doesnt work. can somebody help me? thanks in advance
danpost danpost

2014/12/13

#
The Actor variable 'f' is proabably not finding and object at the offset given. It may be that you think you are using world coordinates (300, 300) when in fact you are using offsets (getX()+300, getY()+300) for the location to find the object. If that is the case, use '(300-getX(), 300-getY())'
ds06 ds06

2014/12/13

#
Yes, i in fact missed that is Offset and not world coordinates. But it still doesnt work :/
1
2
3
4
5
6
7
8
9
10
  public void checkMouse(){
        if(Greenfoot.mouseClicked(null) && !gotTarget){
            Actor hero =((Main)getWorld()).one;
            Actor f = getOneObjectAtOffset(300-hero.getX(),300-hero.getY(),Gegner1.class);
            getWorld().addObject(new Spell3(), f.getX(), f.getY());
            gotTarget=true;
 
         
    }
}
the line 5 is marked by the NPE as Problem.
danpost danpost

2014/12/13

#
If you are trying to look at world coordinates (300, 300), then if you use the 'getOneObjectAtOffset' method, the offset should be with respect to 'this' actor -- you should be using (300-getX(), 300-getY()). If you think 'hero' is 'this', and they produce different results, that 'hero' is not set to 'this'.
ds06 ds06

2014/12/13

#
hm i thougth i had to use the "Actor hero" because "one" is an object of the class "Held". And "Held" is an subclass of "Klassen". And in "Klassen" is where i wrote this code. I dont really know if this makes sense to you :D Anyway, i tried it that way:
1
2
3
4
5
6
7
8
9
public void checkMouse(){
       if(Greenfoot.mouseClicked(null) && !gotTarget){
 
           Actor f = getOneObjectAtOffset(300-getX(),300-getY(),Gegner1.class);
           getWorld().addObject(new Spell3(), f.getX(), f.getY());
           gotTarget=true;
 
       }
   }
still not working :(
danpost danpost

2014/12/13

#
(1) is your Gegner1 object ever not in the world while any of the objects of type Klassen (which includes any object created from any subclass of Klassen) is in the world? (2) what object types are allowed to create a Spell3 object?
ds06 ds06

2014/12/13

#
(1) No. The object "one"(my charakter) of the class "Held"(subclass of "Klassen) is created ad the beginning and on the world all the time:
1
addObject(one, Start.z/2,Start.q/2);
In the World class. And an Object of Gegner1 is also created at the beginning:
1
addObject(new Gegner1(), Start.z/2+50,Start.q/2);
(2)I dont understand the question, sorry:/
danpost danpost

2014/12/13

#
Alright. is your one Held object the only object created from either the Held class or the Klassen class? How are 'Start.q' and 'Start.z' defined and assigned values in your Start class?
ds06 ds06

2014/12/13

#
Yes, ist the only object.
1
2
3
4
private static final double x = (double)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/(double)1.4;
    private static final double y = (double)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/(double)1.4;
    public static final int z = (int)x;
    public static final int q = (int) y;
So one is created in the middle of the screen, and the new Gegner1 Object 50pixels next to it.
danpost danpost

2014/12/13

#
Why were you trying to find the object at (300, 300) then? try (Start.z/2+50-getX(), Start.q/2-getY()).
ds06 ds06

2014/12/13

#
ahhhh okay it works now thanks, youre the best :) its not exactly what i was trying to do but now i understand what was wrong and how this works :D
danpost danpost

2014/12/13

#
BTW, if both of the objects (the Held object and the Gegner1 object) are fixed at their initial locations in the world, then you can just use (50, 0).
You need to login to post a reply.