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

4 days ago

Why is there a null pointer exception

GreenfootNewbie2000 GreenfootNewbie2000

4 days ago

#
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

4 days ago

#
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);
}
GreenfootNewbie2000 GreenfootNewbie2000

4 days ago

#
Thanks for your answer, but I want the following action: If an "old" cloud is removed, a new one should be added
Yehuda Yehuda

4 days ago

#
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

4 days ago

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

4 days ago

#
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

4 days ago

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

2 days ago

#
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

2 days ago

#
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

yesterday

#
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

yesterday

#
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

yesterday

#
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

yesterday

#
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

yesterday

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