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

2012/4/19

Null pointer exception in making an actor point at another actor

Drew Drew

2012/4/19

#
Here's my code: public void pointAtObject() { Actor h = getOneObjectAtOffset(0,0,House.class); setRotation((int)(Math.atan2(h.getY()-getY(),h.getX()-getX())/Math.PI); } Here's the output: java.lang.NullPointerException at Player.pointAtObject(Player.java:36) at Player.checkKeys(Player.java:30) at Player.act(Player.java:7) at greenfoot.core.Simulation.actActor(Simulation.java:498) at greenfoot.core.Simulation.runOneLoop(Simulation.java:461) at greenfoot.core.Simulation.runContent(Simulation.java:197) at greenfoot.core.Simulation.run(Simulation.java:187) The pointAtObject() method is inside the actor that will be pointing, and it is supposed to point to an actor named "House."
davmac davmac

2012/4/19

#
'getOneObjectAtOffset' can return null if there's no object at the specified offset. It looks like you probably meant to use a different method. Also, when you say 'an actor named "House"', I think what you mean is, 'an actor of class House'; that is, 'House' is the name of the class, not the actor. If there's only one instance of House, you can use "getWorld().getObjects(House.class).get(0)" to get a reference to it.
Drew Drew

2012/4/20

#
Thanks, but now it's saying that it got an object and it expects an actor. public void pointAtObject() { Actor h = getWorld().getObjects(House.class).get(0); setRotation((int)(Math.atan2(h.getY()-getY(),h.getX()-getX())/Math.PI)); }
nccb nccb

2012/4/20

#
The list that getObjects back only contains actors, but the compiler doesn't know that, and only knows it has objects. To help the compiler understand, you need what's called a cast, to make sure that you have an Actor. So add Actor in brackets before the get part, like this:
   public void pointAtObject() 
    {
        Actor h = (Actor)getWorld().getObjects(House.class).get(0);
        setRotation((int)(Math.atan2(h.getY()-getY(),h.getX()-getX())/Math.PI));
    }
Drew Drew

2012/4/20

#
Thanks, my spider points at the house whenever it acts now. I'm trying to make the player point at the mouse now, and I get yet another null pointer exception from this: public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); int mouseX=mouse.getX();//From here int mouseY=mouse.getY();//And here }
davmac davmac

2012/4/20

#
Again, Greenfoot.getMouseInfo() can return null - so you need to check for that before you call mouse.getX() and mouse.getY(). if (mouse != null) { ... }
SullyFish SullyFish

2014/5/13

#
Im sorry I dont mean to steal this post, but Im trying to use this code and it doesnt follow my actor for me do I need to follow it up with a different line of code?
SullyFish SullyFish

2014/5/13

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.List; import java.lang.Math; /** * A 'Body' is any kind of object in space that has a mass. It could be a star, or a planet, * or anything else that floats around in space. * * @author Michael Kölling * @version 1.0 */ public class Bat extends SmoothMover { int countdown = 20; public void act(){ countdown --; Greenfoot.delay(); if(countdown >=0){ pointAtObject(); move(3); } } public void pointAtObject() { Actor h = (Actor)getWorld().getObjects(Ninja.class).get(0); setRotation((int)(Math.atan2(h.getY()-getY(),h.getX()-getX())/Math.PI)); } }
danpost danpost

2014/5/13

#
@SullyFish, you should avoid using 'Greenfoot.delay' in scenarios that require constant movement by actors. You will probably have to adjust your 'countdown' value to compensate. BTW, this discussion is two years old and there is now a 'turnTowards' method in the Actor class. Your 'pointAtObject' method can be written like this now:
public void pointAtObject()
{
    Actor h = (Actor)getWorld().getObjects(Ninja.class).get(0);
    turnTowards(h.getX(), h.getY());
}
You need to login to post a reply.