tyler99b
tyler99b

Reputation: 13

Game display null pointer exception

I am trying to display a black window yet I keep getting null pointer exception on line 72 and 43. This is the base for my video game and I have been using tutorials to help me along since I am new to java. It started as an unreachable code error but I fixed that by return and then this problem immediately came up any help? Code:

package com.tyler99b.platformer.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable
{

    private static final long serialVersionUID = 506346024107270629L;

    private boolean running = false;
    private Thread thread;

    public synchronized void start(){
        if(running)
            return;

        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void run()
    {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int updates = 0;
        int frames = 0;
        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1){
                tick();
                updates++;
                delta--;
            }
            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                System.out.println("FPS: " + frames + " TICKS: " + updates);
                frames = 0;
                updates = 0;
            }
        }

    }

    private void tick()
    {

    }

    private void render()
    {


        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null);
        {
            this.createBufferStrategy(3);

        }

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0,0, getWidth(), getHeight());

        g.dispose();
        bs.show();


    }

    public static void main(String args[]){
        new Window(800,600, "Platformer Prototype", new Game ());
    }
}

Upvotes: 1

Views: 450

Answers (2)

Richard Tingle
Richard Tingle

Reputation: 17226

It appears you're using a common pattern of creating something if its null. However, here you set bs to getBufferStrategy() and if its null you create it. Lets assume that function is successful. bs is still null

BufferStrategy bs = this.getBufferStrategy(); 
if (bs == null){
       this.createBufferStrategy(3); 
       //bs still null
}

You need to re attempted to set bs equal to something

BufferStrategy bs = this.getBufferStrategy(); 
if (bs == null) {
       this.createBufferStrategy(3); 
       bs = this.getBufferStrategy(); 
}

All this assumes createBufferStrategy cannot fail. If it can you'll have to decide what to do in that case

Other notes

Your if statement also has a stay ; in it. This makes it an empty if statement

Upvotes: 2

developerwjk
developerwjk

Reputation: 8659

You are definitely missing an else in dealing with bs == null.

    if(bs == null) //there should not be a semi-colon here;
    {
        this.createBufferStrategy(3);
    }
    else
    {
        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0,0, getWidth(), getHeight());

        g.dispose();
        bs.show();
     }

Upvotes: 0

Related Questions