Hello! I'm having a problem deleting an Object at a specific position after i release my mouse. (after a mouse drag).
I'm Currently doing this: (the first couple If's are for mouse dragging, Take a look at the last one. mouseDragEnded(this)
What could be the problem?
I'm getting this error:
java.lang.NullPointerException at Dijk.act(Dijk.java:82) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
import greenfoot.*;
public class Dijk extends DijkenBouwen
{
private boolean isGrabbed;
private int health = 10;
public int getHealth()
{
return health;
}
public void setHealth(int health)
{
this.health = health;
}
public void act()
{
if (Greenfoot.mousePressed(this) && !isGrabbed)
{
isGrabbed = true;
World world = getWorld();
MouseInfo mi = Greenfoot.getMouseInfo();
world.removeObject(this);
world.addObject(this, mi.getX(), mi.getY());
return;
}
if ((Greenfoot.mouseDragged(this)) && isGrabbed)
{
MouseInfo mi = Greenfoot.getMouseInfo();
setLocation(mi.getX(), mi.getY());
return;
}
if (Greenfoot.mouseDragEnded(this) && isGrabbed)
{
isGrabbed = false;
//Bouw plaats
Actor actor = getOneIntersectingObject(buildPlace.class);
//Resource groot
Actor actor2 = getOneIntersectingObject(resBig.class);
//Dijk al gebouwd
Actor actor3 = getOneIntersectingObject(Dijk1.class);
if(actor != null && actor2 == null && actor3 == null)
{
int y = actor.getY();
int x = actor.getX();
Dijk1 dijk = new Dijk1();
getWorld().addObject(dijk,x,y);
getWorld().removeObject(this);
}
else if(actor3 != null)
{
int y = actor3.getY();
int x = actor3.getX();
Dijk2 dijk2 = new Dijk2();
getWorld().addObject(dijk2,x,y);
getWorld().removeObject(this);
getWorld().removeObjects(getWorld().getObjectsAt(x,y,Dijk1.class));
}
return;
}
}
}