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

2017/5/19

Why is there a null pointer exception

Hi,
if(isAtEdge()==true){
    getWorld().removeObject(this);
getWorld().addObject(new cloud(),spawnX,spawnY);
I get there, in the second and third line a null pointer exception Could someone explain me why and how can I fix it Thanks for help
Yehuda Yehuda

2017/5/19

#
Since you just removed the Cloud from the World you can't call the getWorld() method (since there is none). Try changing the code to this:
if (isAtEdge()) {
    getWorld().addObject(new Cloud(), spawnX, spawnY);
    getWorld().removeObject(this);
}
Thanks for your answer, but I want the following action: If an "old" cloud is removed, a new one should be added
Yehuda Yehuda

2017/5/19

#
GreenfootNewbie2000 wrote...
Thanks for your answer, but I want the following action: If an "old" cloud is removed, a new one should be added
Why do you think that's not what will happen?(!) You asked a question and I answered it. Now you're asking on me without even trying what I said (or you did try it, but you just didn't notice that I wrote 'new Cloud()'.
danpost danpost

2017/5/19

#
More simply would be:
if (isAtEdge()) setLocation(spawnX, spawnY);
Why remove and add when you can just restart the old one!
Yehuda Yehuda

2017/5/19

#
danpost wrote...
More simply would be:
if (isAtEdge()) setLocation(spawnX, spawnY);
Why remove and add when you can just restart the old one!
Don't ask me. (But I don't know that you were.)
danpost danpost

2017/5/19

#
Yehuda wrote...
Don't ask me. (But I don't know that you were.)
Cryptic! That I was what?
Yehuda Yehuda

2017/5/21

#
danpost wrote...
Yehuda wrote...
Don't ask me. (But I don't know that you were.)
Cryptic!
What do you mean by that?
That I was what?
I said "Don't ask me.", but I don't know for sure that you were asking me, it could be you were asking GreenfootNewbie2000.
danpost danpost

2017/5/21

#
Yehuda wrote...
I don't know for sure that you were asking me, it could be you were asking GreenfootNewbie2000.
Where did I ask a question in my original post?
Yehuda Yehuda

2017/5/21

#
danpost wrote...
Where did I ask a question in my original post?
The question I was referring to is:
danpost wrote...
Why remove and add when you can just restart the old one!
The sentence starts with the word "Why". I was saying that it wasn't my idea to do that (next post).
danpost danpost

2017/5/21

#
Yehuda wrote...
The sentence starts with the word "Why". I was saying that it wasn't my idea to do that (next post).
But it ends with an exclamation point, making it an exclamation, not a question (making a point of it)!
Yehuda Yehuda

2017/5/21

#
danpost wrote...
But it ends with an exclamation point, making it an exclamation, not a question (making a point of it)!
I know it wasn't a question that was looking for an answer, and I was agreeing, by saying that I can't answer the (rhetorical) question.
danpost danpost

2017/5/21

#
Yehuda wrote...
danpost wrote...
But it ends with an exclamation point, making it an exclamation, not a question (making a point of it)!
I know it wasn't a question that was looking for an answer, and I was agreeing, by saying that I can't answer the (rhetorical) question.
Oh, please stop!
Yehuda Yehuda

2017/5/21

#
danpost wrote...
Oh, please stop!
Done.
You need to login to post a reply.