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

2019/3/3

Error: non-static method cannot be referrenced from a static context

PolarBear123 PolarBear123

2019/3/3

#
Hello! I have bought the Greenfoot book and I'm already at Chapter 9.10. There, I'm at exercise 9.69, which is a final excersise. I'm trying to add some score to my Asteroids game. When I'm trying to call the add(score) method (which belongs to the Counter class) from the World (Space), I get this error: non-static method cannot be referenced from a static context. I don't quite understand how to fix it, and there isn't a 'solution' in the book. (It's the final exercise). Here is some of the code: Counter class:
1
2
3
4
5
6
7
8
/**
 * Add a new score to the current counter value.  This will animate
 * the counter over consecutive frames until it reaches the new value.
 */
public void add(int score) //This is the add(score) class from the method Counter. It adds
{                                        //the score if you type it in the parameter section.
    target += score;
}
Space class:
1
2
3
4
5
6
7
public void countScore(int score)//This method belongs to the world. When you
/**                                                         //add the score here, it automatically should get added to the Counter class.
 * Count the score.
 */
{
    Counter.add(score); //Here is where I get my error. I don't understand why.
}
Calling the countScore(score) method from the Asteroid class:
1
2
Space space = (Space) getWorld();//Here it's supposed to add 20 points to the score whenever a bullet touches an asteroid. I don't have any errors here.
space.countScore(20);
I hope I got the formatting right as this is my first post. Thank you!
danpost danpost

2019/3/3

#
Please show the line in your Space class that declares a Counter field (should be outside any method and begin with 'Counter', 'public Counter' or 'private Counter').
PolarBear123 PolarBear123

2019/3/3

#
That might be my problem... how would one of those fields look like?
danpost danpost

2019/3/3

#
PolarBear123 wrote...
That might be my problem... how would one of those fields look like?
1
2
3
4
5
Counter counter = new Counter();
// or
private Counter counter = new Counter();
// or
public Counter counter = new Counter();
PolarBear123 PolarBear123

2019/3/3

#
Yes, so I found my declaration of the counter field att the top of the class:
1
Counter counter = new Counter()
I've tried changing it to private... or public... but the issue persists.
danpost danpost

2019/3/3

#
PolarBear123 wrote...
Yes, so I found my declaration of the counter field att the top of the class: << Code Omitted >> I've tried changing it to private... or public... but the issue persists.
Okay, the countScore method in your Space class should use:
1
counter.add(score);
PolarBear123 PolarBear123

2019/3/4

#
Ok, that error is fixed. But now, every time an asteroid get's hit, a popup window appears which gives me this error:
1
2
3
4
5
6
7
8
java.lang.NullPointerException
    at Asteroid.hit(Asteroid.java:80)
    at Bullet.checkAsteroidHit(Bullet.java:52)
    at Bullet.act(Bullet.java:39)
    at greenfoot.core.Simulation.actActor(Simulation.java:567)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
    at greenfoot.core.Simulation.runContent(Simulation.java:193)
    at greenfoot.core.Simulation.run(Simulation.java:183)
Thank you and I hope I'm not bothering you guys with these noob questions.
danpost danpost

2019/3/4

#
PolarBear123 wrote...
Ok, that error is fixed. But now, every time an asteroid get's hit, a popup window appears which gives me this error: << Error Trace Omitted >> Thank you and I hope I'm not bothering you guys with these noob questions.
You will need to show the act and checkAsteroidHit methods in your Bullet class plus the hit method in your Asteroid class.
PolarBear123 PolarBear123

2019/3/4

#
Yes, but the methods are already declared: Bullet class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * The bullet will damage asteroids if it hits them.
 */
public void act()//act class
{
    if(life <= 0) {
        getWorld().removeObject(this);
    }
    else {
        life--;
        move();
        checkAsteroidHit();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Check whether we have hit an asteroid.
 */
private void checkAsteroidHit()//checkAsteroidHit class
{
    Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
    if (asteroid != null)
    {
        getWorld().removeObject(this);
        asteroid.hit(damage);
    }
}
Same with the hit class from Asteroid. Where would I show them?
danpost danpost

2019/3/4

#
PolarBear123 wrote...
Same with the hit class from Asteroid. Where would I show them?
Here. In fact, that is the main one I wanted to see.
PolarBear123 PolarBear123

2019/3/5

#
Ok. I guess by here you meant this forum: checkAsteroidHit | Bullet
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Check whether we have hit an asteroid.
 */
private void checkAsteroidHit()
{
    Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
    if (asteroid != null)
    {
        getWorld().removeObject(this);
        asteroid.hit(damage);
    }
}
act | Bullet /** * The bullet will damage asteroids if it hits them. */ public void act() { if(life <= 0) { getWorld().removeObject(this); } else { life--; move(); checkAsteroidHit(); } } hit | Asteroid
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * Hit this asteroid dealing the given amount of damage.
 */
public void hit(int damage)
{
    stability = stability - damage;
    if (stability <= 0)
    {
        breakUp();
        Space space = (Space) getWorld();
        space.countScore(20);
    }
}
danpost danpost

2019/3/5

#
Move line 9 in the hit method down two lines. I have a feeling it removes the asteroid and replaces it with new smaller ones. With this one removed from the world, line 10 will set space to null. By moving the line down, it will still be in the world when line 10 executes.
PolarBear123 PolarBear123

2019/3/5

#
Thank you! that fixed my error.
You need to login to post a reply.