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

2020/1/14

Keep Getting Null Pointer Execption

Viransh Viransh

2020/1/14

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ private Score score; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(640,470, 1); addObject(new Snake(),321,216); addObject(new Food(),Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(430)); addObject(new Score(), 320, 450); } public void spawnFood(){ addObject(new Food(),Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(430)); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Food here. * * @author (your name) * @version (a version number or a date) */ public class Food extends Actor { /** * Act - do whatever the Food wants to do. Thi s method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private GreenfootImage foods = new GreenfootImage; private int random = Greenfoot.getRandomNumber(3); public Food(){ for(int i = 0; i < foods.length; i++){ foods = new GreenfootImage("Food" + i + ".png"); } setImage(foods); } public void act() { } public GreenfootImage getFoodImage(int food){ if(food == 0){ return foods; } else if(food == 1){ return foods; } return foods; } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Score here. * * @author (your name) * @version (a version number or a date) */ public class Score extends Actor { /** * Act - do whatever the Score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int score; public Score(){ updateImage(); } public void act() { // Add your action code here. } public void changeScore(){ score++; updateImage(); } public void updateImage(){ setImage(new GreenfootImage("Score: " + score, 24, Color.RED, Color.WHITE)); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Snake here. * * @author (your name) * @version (a version number or a date) */ public class Snake extends Actor { /** * Act - do whatever the Snake wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private Food food; private Score score; private int speed; private boolean check; public Snake(){ GreenfootImage snake = getImage(); snake.scale(snake.getWidth() - 155, snake.getHeight() - 110); setImage(snake); } public void act() { control(); check(); move(25); } public void control(){ if(Greenfoot.isKeyDown("right") && getRotation() != 0 && getRotation() != 180){ setRotation(0); } if(Greenfoot.isKeyDown("left") && getRotation() != 180 && getRotation() != 0){ setRotation(180); } if(Greenfoot.isKeyDown("up") && getRotation() != 270 && getRotation() != 90){ setRotation(270); } if(Greenfoot.isKeyDown("down") && getRotation() != 90 && getRotation() != 270){ setRotation(90); } } public void check(){ if(isTouching(Food.class)){ removeTouching(Food.class); ((MyWorld)getWorld()).spawnFood(); score.changeScore(); } } } I keep getting this error: java.lang.NullPointerException at Snake.check(Snake.java:49) at Snake.act(Snake.java:28) 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)
danpost danpost

2020/1/14

#
Viransh wrote...
I keep getting this error: java.lang.NullPointerException at Snake.check(Snake.java:49) at Snake.act(Snake.java:28)
You never assign ANY Score object to the score field in your Snake class. And, of course, you would need to assign the one in your world to it (not a different or new one).
Viransh Viransh

2020/1/14

#
I don't understand how to do that. I already initialized private Food food; and private Score score;. What else should I do?
danpost danpost

2020/1/14

#
Viransh wrote...
I don't understand how to do that. I already initialized private Food food; and private Score score;. What else should I do?
No. You are only declaring that you will be using them in your Snake class. However, their values will remain null until objects are assigned to them.
Viransh Viransh

2020/1/14

#
How exactly do I assign the same objects that are already created in MyWorld? Please help me.
danpost danpost

2020/1/15

#
Viransh wrote...
How exactly do I assign the same objects that are already created in MyWorld? Please help me.
You could try this:
if (score == null) score = (Score)getWorld().getObjects(Score.class).get(0);
at the beginning of your Snake act method, with something similar for the Food object.
You need to login to post a reply.