super.paintComponent(g)上的java.lang.NullPointerException [英] java.lang.NullPointerException on super.paintComponent(g);

查看:96
本文介绍了super.paintComponent(g)上的java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在super.paintComponent(e)行上收到java.lang.NullPointerException,我看了一些教程,但不确定自己在做什么.

I'm getting a java.lang.NullPointerException on my super.paintComponent(e) line, I have looked at a few tutorials but I'm unsure of what I'm doing wrong.

这里是我的主班:

package pack;

public class Main  {    
public static void main(String[] args){     
    @SuppressWarnings("unused")
    Display display = new Display("The JFrame", 510, 510);          
    Game game = new Game();
    game.start();
}
}

显示类别:

    package pack;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Display {
private JFrame frame;
private String title;
private int width, height;
private static JPanel canvas;

public Display(String title, int width, int height) {
    this.title = title;
    this.width = width;
    this.height = height;       
    createDisplay(); 
}

private void createDisplay(){       
    frame = new JFrame(title);   
    frame.setSize(width, height); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true);             
    canvas = new JPanel();                  
    canvas.setPreferredSize(new Dimension(width, height)); 
    canvas.setMaximumSize(new Dimension(width, height));   
    canvas.setMinimumSize(new Dimension(width, height));    
    frame.add(canvas);          
    frame.pack();           
    }

public static JPanel getCanvas(){       
    return canvas;              
}
}

最后确定发生错误的Game类:

And finaly the Game class where the error occurs:

    package pack;

import java.awt.Graphics;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable {

private Thread thread;
private boolean running = false;
public Graphics e;

public synchronized void stop() {       
if(running = false){
    return;         
}       
running = false;    
try {
        thread.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}  

public synchronized void start() {      
    if(running)
        return;     
    running = true;
    thread = new Thread(this);  
    thread.start();     
}   

@Override
public void run() {     
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
@SuppressWarnings("unused")
int ticks = 0;
long timer = 0; 
while(running)  {       
    now = System.nanoTime();        
    delta += (now - lastTime) / timePerTick;        
    timer += now - lastTime;        
    lastTime = now;     
    if(delta >= 1){ 

        paintComponent(e);  //ERROR LINE

ticks++;    
delta--;    
    }   
    if(timer >= 1000000000){
        ticks = 0;
        timer = 0;
    }   
 }
stop();
}   

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);                    
}       
}

如果我替换

super.paintComponent(e) 

g = Display.getCanvas().getGraphics(); 

该错误不再发生.

推荐答案

更改:

paintComponent(g);

收件人:

repaint(); //will cause a call to paintComponent with a VALID graphics instance

有效的实现.除了纠正错误外,此代码还将Game的实例添加到框架中以便可见,并在paintComponent(..)覆盖中执行了一些有用的操作,以便可以看到它执行任何操作.

Working implementation. Besides fixing the error, this code adds an instance of the Game to the frame so it is visible, and does something useful in the paintComponent(..) override so that it can be seen to do anything.

import java.awt.*;
import javax.swing.*;

public class Display {

    private JFrame frame;
    private String title;
    private int width, height;
    private static Game canvas;

    public Display(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;
        createDisplay();
    }

    private void createDisplay() {
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        canvas = new Game();
        canvas.start();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));
        frame.add(canvas);
        frame.pack();
    }

    public static JPanel getCanvas() {
        return canvas;
    }

    public static void main(String[] args) {
        Display display = new Display("The JFrame", 510, 510);
    }
}

class Game extends JPanel implements Runnable {

    private Thread thread;
    private boolean running = false;

    public synchronized void stop() {
        if (running = false) {
            return;
        }
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void start() {
        if (running) {
            return;
        }
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        int fps = 60;
        double timePerTick = 1000000000 / fps;
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();
        @SuppressWarnings("unused")
        int ticks = 0;
        long timer = 0;
        while (running) {
            now = System.nanoTime();
            delta += (now - lastTime) / timePerTick;
            timer += now - lastTime;
            lastTime = now;
            if (delta >= 1) {

                //paintComponent(e);  //ERROR LINE
                repaint();

                ticks++;
                delta--;
            }
            if (timer >= 1000000000) {
                ticks = 0;
                timer = 0;
            }
        }
        stop();
    }

    int x=0;
    int y=0;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(0, 0, x++, y++);
    }
}

这篇关于super.paintComponent(g)上的java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆