user2985427
user2985427

Reputation: 1

DrawString Not Drawing String

Hello I am currently learning java game development and when I run the code it runs but does not draw String it gives me this error Exception in thread "main" java.lang.NullPointerException at Code.Core.render(Core.java:62) at Code.Core.run(Core.java:69) at Code.Core.start(Core.java:27) at Code.Core.main(Core.java:45)

Heres my Code

package Code;

import java.applet.Applet;
import java.awt.*;
import java.awt.image.VolatileImage;

import javax.swing.*;

public class Core extends Applet implements Runnable{   
////////////////////////////////////////////////////////////////////
    double x, y, Cx, Cy;

    public static boolean isRunning = false;

    static JFrame frame;

    public static Core core = new Core();

    public Image i = new ImageIcon("res/Google.jpg").getImage();

    public VolatileImage screen;
////////////////////////////////////////////////////////////////////


    public void start() {
        isRunning = true;
        core.run();
    }

    public void stop() {
        isRunning = false;
    }

    public static void main(String[] args) {
        frame = new JFrame();

        frame.add(core);
        frame.setUndecorated(true);
        frame.pack();
        frame.setResizable(false);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        core.start();


    }



    public void tick() {
        System.out.println("tick");
    }

    public void render() {
        Graphics g = null;
        System.out.println("render");
        setBackground(Color.PINK);
        setForeground(Color.WHITE);

        g.drawString("Test", 500, 500);

    }

    public void run() {
        while(isRunning){
            tick();
            render();
            try{Thread.sleep(5);}catch(Exception e){isRunning = false;}

        }

    }
}

Upvotes: 0

Views: 133

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347332

  1. You are extending from Applet, but creating a JFrame to house it. It's npt advisable to mix heavy and light weight components, let alone add an applet to a frame.
  2. You are creating a long running loop without consideration to the context of the thread you are running it, potentially causing it to block the Event Dispatching Thread, preventing any possible paint updates (amongst other things)
  3. Your render method has not valid Graphics content with which to paint to...

Start by creating a custom component onto which you can paint (extending from something like JPanel for example). You can then decide where you want to display it, such as on an applet or frame.

Take a look at Performing Custom Painting for details about how to perform painting in Swing

Take a look at Concurrency in Swing for details about how to perform long running tasks and update the UI from out side the context of the Event Dispatching Thread

Upvotes: 0

sandymatt
sandymatt

Reputation: 5612

public void render() {
    Graphics g = null;              // uh oh.
    System.out.println("render");
    setBackground(Color.PINK);
    setForeground(Color.WHITE);
    g.drawString("Test", 500, 500); // g is null here.
}

You're assigning g to null directly, and then calling a method on it. That will give you an NPE.

Upvotes: 2

Related Questions