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

2021/3/2

Having a problem with a Null Pointer Exception.

DeUlo DeUlo

2021/3/2

#
The part where the problem occured is in an "attack" method that is supposed to subtract some hp from the hit enemy. I get a Null Pointer Exception at the if(getOneObjectAtOffset(getImage().getWidth()/2,0,ENEMY.class).getClass() == ENEMY.class) the rest of the code works as it's supposed to when I replace the inside with a isTouching(), though that doesn't do what I need it to work properly. timerStarted2 is from the SimpleTimer class.
if(mouse.getX()>=getX())
                {
                    this.setImage(attack1R.getCurrentImage());
                    if ((getImage() != imageOne) != notImageOne)
                    {
                        notImageOne = ! notImageOne;
                        if (!notImageOne) 
                        {
                            this.setImage(attack1R.getImages().get(4));
                            attack= false;
                            setLocation(getX(),getY()+20);
                        }
                    }
                    if(getOneObjectAtOffset(getImage().getWidth()/2,0,ENEMY.class).getClass() == ENEMY.class) 
                    {
                        if(!timerStarted2)
                        {
                            timerStarted2 = !timerStarted2;
                            timer2.mark();
                        }
                        ENEMY enemy = (ENEMY) getOneIntersectingObject(ENEMY.class);
                        if(timer2.millisElapsed() > 200 || enemy.leben == enemy.maxLeben)
                        {
                            enemy.changeLives(-1);
                            timerStarted2 = !timerStarted2;
                        }

                    }
                }

danpost danpost

2021/3/3

#
Use:
if (getOneObjectAtOffset(getimage().getWidth()/2, 0, ENEMY.class) != null)
If it is not null, it WILL be an ENEMY object.
DeUlo DeUlo

2021/3/3

#
danpost wrote...
Use:
if (getOneObjectAtOffset(getimage().getWidth()/2, 0, ENEMY.class) != null)
If it is not null, it WILL be an ENEMY object.
Thank you so much! I appreciate it!
You need to login to post a reply.