-1

I'm making a bee game and so far its all been going well until i've tried to add my bee character where i've stumbled across a problem. The problem is it doesn't add my characater into the stage so I cant see my bee character and I dont know why? I'm not sure what the problem is the code seems to be fine to me and i've asked some of my friends and they don't why it breaks either?

i get this error :

Caused by: java.lang.NullPointerException
    at myFirstGame.GameObject.update(GameObject.java:20)
    at myFirstGame.BeeCharacter.update(honeyBee.java:88)
    at myFirstGame.BeeCharacter.<init>(honeyBee.java:79)
    at myFirstGame.Factory.createCharacter(honeyBee.java:109)
    at myFirstGame.honeyBee.start(honeyBee.java:65)

my beeClass

package myFirstGame;

import java.util.ArrayList;
import java.util.Random;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class honeyBee extends Application {

    GameObject gameObject;
    GraphicsContext gc;
    Stage stage;
    Canvas canvas;
    Image img;
    Factory Factory;



    ArrayList<GameObject>characterList = new ArrayList<GameObject>();
    Random rnd = new Random(System.currentTimeMillis());
    int count = 0;

    AnimationTimer timer = new AnimationTimer() {

        @Override
        public void handle(long now) {
            // TODO Auto-generated method stub
            gc.drawImage(img, 0, 0, canvas.getWidth(), canvas.getHeight());
            for(GameObject obj : characterList) {
                obj.update();
            }

        }
    };

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        launch(args);
    }

    public void start(Stage stage) throws Exception {
        stage.setTitle("HoneyBee");
        Pane root = new Pane();
        Factory = new Factory(gc);
        stage.setScene(new Scene(root, 780, 580));
        stage.setResizable(false);
        stage.show();


        canvas = new Canvas(800,600);
        gc = canvas.getGraphicsContext2D();
        img =new Image("res/1.png");
        gc.drawImage(img, 0, 0, canvas.getWidth(), canvas.getHeight());
        root.getChildren().add(canvas);


        //gameObject.timer.start();
        characterList.add(Factory.createCharacter("bee", 30, 30));

        timer.start();
    }
}

class BeeCharacter extends GameObject{

    double dx=1;

    public BeeCharacter(GraphicsContext gc, double x, double y) {

        super(gc, x, y);
        img = new Image("res/bee.png");
        update();
    }

    public void update() {
        x+=dx;
        if(x>800 || x<0) {
            dx=-dx;
            y+=20;
        }
        super.update();
    }
}

interface FactoryIF {
    GameObject createCharacter(String start, double x, double y);
}


class Factory implements FactoryIF {

    GraphicsContext gc;

    public Factory(GraphicsContext gc) {
        super();
        this.gc = gc;
    }
    @Override
    public GameObject createCharacter(String start, double x, double y) {
        // TODO Auto-generated method stub
        if(start.equals("bee")) 
            return new BeeCharacter(gc, x, y);
        else
            return null;
    }

}

my GameObject class

package myFirstGame;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

    class GameObject {
    protected Image img;
    protected double x, y;
    protected GraphicsContext gc;

    public GameObject(GraphicsContext gc, double x, double y){
    this.gc=gc;
    this.x=x;
    this.y=y;
    }

    public void update(){

    if(img!=null)
        gc.drawImage(img, x, y, 30, 30);
    }


}

  • 1
    You are passing an uninitialized `GraphicsContext` to `GameObject` so of course you will get a NPE when you use it. – user1803551 Apr 29 '19 at 11:38

1 Answers1

0

I put Factory = new Factory(gc); under gc = canvas.getGraphicsContext2D(); and it fixed it thank you whoever answered it and then deleted !