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

2017/11/22

My Program gives me a nullpointerexception

TheShyGuy TheShyGuy

2017/11/22

#
for(int i=0; i<8; i++)
                            {
                                Actor x = (Actor) getOneObjectAtOffset(i,0,null);
                                if(!getObjectsAtOffset(i,0,null).isEmpty())
                                {
                                    if(x.getX() > mX)
                                    {
                                        setLocation(mX, mY);
                                        selected = false;
                                        aktion = true;
                                        i=8;
                                    }
                                }
                                else if(i==7)
                                {
                                    setLocation(mX, mY);
                                    selected = false;
                                    aktion = true;
                                }
                            }
Whenever i try to move, it gives me a nullpointerexception, at:
if(x.getX() > mX)
(mX is an int, which displays the x coordinate for where my mouse clicked)
danpost danpost

2017/11/22

#
I cannot see how you are getting that error on that line, especially considering the condition before it (line 4) and that no action is taken between that and the next previous line (line 3) where you assign 'x' its value. However, an improvement that will probably solve the issue is that you really do not need that particular condition on line 4. You can just use this:
if (x != null)
You need to login to post a reply.