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

2013/4/12

java.lang.NullPointerException

welleid welleid

2013/4/12

#
So I have a problem. I have a drop falling, if it touches a plate, something happens oO. My Drop.class has a speed set as
//In act method
setLocation(getX(), getY() + moveY);
moveY is definded in class itself (all above) as a value of 10, not in the act method. Then, in my Plate.class
Drop drop = (Drop)getOneIntersectingObject(Drop.class);  

if(drop != null)
        {
            isSlippy = true;
            bloppy = 5;

        }
The plate becomes Slippy and I set an int variable at 5. In the act method of the Plate.class, I just put a bloppy--; so at the beginning, bloppy = 0, when the drop hits the plate, bloppy = 5 and decrease. Then I added this still in my Plate.class:
if(bloppy > 0)
        {
            drop.moveY = 0;
            drop.setImage("blop.png");
        }
        
        if(bloppy == 0)
        {
            getWorld().removeObject(drop);
        }
But actually, nothing is working. The image of the drop is changed, but it doesnt disappear and I get a java console error message : java.lang.NullPointerException which redirects me to line :
if(bloppy > 0)
        {
            drop.moveY = 0; //This line
            drop.setImage("blop.png");
        }
Thanks a lot guys !
davmac davmac

2013/4/12

#
'drop' is null. You are checking for that in your earlier code:
if(drop != null)  
        {  
            isSlippy = true;  
            bloppy = 5;  
  
        }  
But, your later code is 'outside' the if-statement. You need to check again whether drop is null:
    if(bloppy > 0 && drop != null)  
            {  
                drop.moveY = 0; //This line  
                drop.setImage("blop.png");  
            }  
welleid welleid

2013/4/12

#
Thank you ! I dont have any more error message. but it doesn't dissapear when bloppy gets back to 0^^'
if(drop != null)
        {
            isSlippy = true;
            bloppy = 5;

        }
        
        
        if(bloppy == 0)
        {
            getWorld().removeObject(drop);
        }
        
        else if(bloppy > 0 && drop != null)
        {
            drop.moveY = 0;
            drop.setImage("blop.png");
        }
davmac davmac

2013/4/12

#
You have:
if(drop != null)  
        {  
            isSlippy = true;  
            bloppy = 5;   // THIS
  
        }  
So bloppy is always set to 5 if drop != null.
welleid welleid

2013/4/12

#
EDIT: ok I'm bad. Is there a way Bloppy doesnt stay at 5 all the time ? thanks !
davmac davmac

2013/4/12

#
Well, when do you want it to be 5? That determines where you should set it to 5. Also, do you ever change its value? I'm assuming its meant to be some sort of counter but your code posted above never sets it to anything other than 5. In other words: Can you explain what 'bloppy' is meant for?
welleid welleid

2013/4/12

#
So, bloppy is a variable that is supposed to get a value of 5 whenever a drop hits the plate. Then, in the act method, i put bloppy--; to make it decrease. The purpose of it is that when bloppy is at 5, the picture of the drop changes (its a picture of a drop hitting something, with setImage() ) and when bloppy returns to 0 within the act method, i want this drop to disappear. And when another drop hits again and again^^ Thanks a lot !
danpost danpost

2013/4/12

#
The 'bloppy' code and removal of the Drop object should be done in the Drop class.
// in the Plate class, you can still have
Drop drop = (Drop)getOneIntersectingObject(Drop.class);
if(drop != null)
{
    if (drop.bloppy == 0)
    {
        isSlippy = true;
        drop.setBloppy();
    }
}
else isSippy = false;
// then, in the Drop class act (or method it calls)
if (bloppy == 0)
{
    setLocation(getX(), getY() + moveY);
}
else
{
    bloppy--;
    if (bloppy == 0) getWorld().removeObject(this);
}
// and add this method
public void setBloppy()
{
    bloppy = 5;
    setImage("blop.png");
}
welleid welleid

2013/4/12

#
Thanks man ! Your solution didnt work at all but gave me some clues to do it !
welleid welleid

2013/4/12

#
Oh and I have a last question, nothing to see with it, but it often happens.. what does this error message mean ? java.lang.OutOfMemoryError: Java heap space
danpost danpost

2013/4/12

#
That usually means you have you have something in the wrong place. Possibly something in the act method (or a method it calls) that should be either in the constructor of the object or in the 'started' or 'addedToWorld' method. Another possibility is not regulation the number of objects that are created in the act method (one being created every act frame). Without seeing your complete error message and the related code, it would be difficult to help you further.
You need to login to post a reply.