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

2012/4/4

removing objects at the left of screen

1
2
3
4
e_e13 e_e13

2012/4/7

#
I already did that later on in the code, does moving them up change it? Also, by giving the variables a value, do you mean a number? Where do I add the number to 'counter = new Counter();'?
SPower SPower

2012/4/7

#
No, a value isn't always a number. Let me explain: when you create a counter, like this:
private Counter counter;
you're just telling the compiler there is a counter, so that it can reserve some memory for it. But that memoy is still empty. What you put in that memory, that's the value (that's important to understand :) ). But when you use empty memory (variable without value), you'll get null pointer exception(that's also important to understand :) ). And I see that you give the counters a value in the action game(), but I don't see you using this method. Do you use that method before you return a counter? Otherwise: null pointer exception.
e_e13 e_e13

2012/4/7

#
Ah okay, I see. The error code in question refers to null pointer exception, but the two lines it refers to (spaceWorld.finish(); in Counter class and counter.bumpCount(); in the Bullet class) don't appear to be able to hold values (since the methods they refer to are void). I'm completely stumped :/
SPower SPower

2012/4/7

#
Just try to move those lines:
counter = new Counter();
li = new Counter();
to public spaceinvaders(), that might help. Sometimes the showed error is something else than where it really should be.
e_e13 e_e13

2012/4/7

#
Nope, I'm still getting the same error
danpost danpost

2012/4/7

#
'void' just means that the method will not return an object (whether it be an integer object or an actor object or whatever object) back to the calling method. You have the method 'game()' in your spaceinvaders(world) class, however, it is not being called; and since you are setting the "value" of your counters in that method, they are not being set.
danpost danpost

2012/4/7

#
Actually, I see several methods that are not being called, namely 'game()', 'add()', 'add2()', and 'add3()'. Looking at the 'game()' method, it appears you have an instance of all your actor class objects being added to the world (except for the counters). I do not think that you want all your actors being added right here. Certainly, a rocket would not be the first object added to your world (you would want that added from your invader class). Only include your initial objects in the game() method; such as (1) your invaders, (2) your player object, and (3) your counters. The other objects (bullet and rocket and spaceship) are added later during play.
e_e13 e_e13

2012/4/7

#
I am calling those methods, only in other classes that I didn't list before. And my rocket is my player object, the bullets are what it shoots so I would count them as part of one actor. In regards to the counter problem, it dosen't seem to have an answer. Shall I start again, and if so what tutorial can I follow?
SPower SPower

2012/4/7

#
It's better to give those variables a value in your world, then you're sure that your variables work fine.
e_e13 e_e13

2012/4/7

#
Are you referring to the two that cause the error? I can't since it tells me it expected void, not int. And by changing the method to accommodate this, what value should I return?
danpost danpost

2012/4/7

#
The error is caused by calling 'getWorld()' in the counter class while the counter is not in the world. e_e13: do not confuse yourself with the void/int thing. If the method is to return information, then you would use the 'type' (the kind of object that is to be returned, whether it be an int or an Actor or a String, etc.) instead of 'void'. The only methods I see that would not use void (other than constructors, which do NOT have a return type) are the 'getLife()' and the 'getCounter()' methods, whose return types are 'Counter' objects.
e_e13 e_e13

2012/4/8

#
Oh okay, but I've moved counter = new Counter(); and li = new Counter(); into public spaceinvaders() and it's still not working. When getWorld is called, isn't spaceinvaders() called- and therefore the counter objects as well?
danpost danpost

2012/4/8

#
When 'getWorld' is called, it tries to return the world that the object in question (the counter) is in. If is has not been 'addObject'ed to the world or it has been 'removeObject'ed from the world, then 'getWorld' will return 'null' and a NullPointerException occurs.
You need to login to post a reply.
1
2
3
4