java指针异常_java空指针异常如何解决

问题补充:

package javagames.input;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.util.*;

import javagames.util.*;

import javax.swing.*;

public class SimpleMouseExample extends JFrame implements Runnable {

private FrameRate frameRate;

private BufferStrategy bs;

private volatile boolean running;

private Thread gameThread;

private SimpleMouseInput mouse;

private KeyboardInput keyboard;

private ArrayList lines = new ArrayList();

private boolean drawingLine;

private Color[] COLORS = { Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE };

private int colorIndex;

public SimpleMouseExample() {

// TODO Auto-generated constructor stub

frameRate = new FrameRate();

}

protected void createAndShowGUI() {

Canvas canvas = new Canvas();

canvas.setSize(640, 480);

canvas.setBackground(Color.BLACK);

canvas.setIgnoreRepaint(true);

getContentPane().add(canvas);

setTitle("Simple Mouse Example");

setIgnoreRepaint(true);

pack();

// Add key listeners

keyboard = new KeyboardInput();

canvas.addKeyListener(keyboard);

// Add mouse listeners

mouse = new SimpleMouseInput();

canvas.addMouseListener(mouse);

canvas.addMouseMotionListener(mouse);

canvas.addMouseWheelListener(mouse);

setVisible(true);

canvas.requestFocus();

gameThread = new Thread(this);

gameThread.start();

}

@Override

public void run() {

// TODO Auto-generated method stub

running = true;

frameRate.initialize();

while (running) {

gameLoop();

}

}

private void gameLoop() {

// TODO Auto-generated method stub

processInput();

renderFrame();

sleep(10L);

}

private void renderFrame() {

// TODO Auto-generated method stub

do {

do {

Graphics g = null;

try {

g = bs.getDrawGraphics();

g.clearRect(0, 0, getWidth(), getHeight());

render(g);

} finally {

if (g != null) {

g.dispose();

}

}

} while (bs.contentsRestored());

bs.show();

} while (bs.contentsLost());

}

private void render(Graphics g) {

// TODO Auto-generated method stub

colorIndex += mouse.getNotches();

Color color = COLORS[Math.abs(colorIndex % COLORS.length)];

g.setColor(color);

frameRate.calculate();

g.drawString(frameRate.getFrameRate(), 30, 30);

g.drawString("Use mouse to draw lines", 30, 45);

g.drawString("Press C to clear lines", 30, 60);

g.drawString("Mouse Wheel cycles colors", 30, 75);

g.drawString(mouse.getPosition().toString(), 30, 90);

for (int i = 0; i < lines.size() - 1; ++i) {

Point p1 = lines.get(i);

Point p2 = lines.get(i + 1);

// Adding a null into the list is used

// for breaking up the lines when

// there are two or more lines

// that are not connected

if (!(p1 == null || p2 == null))

g.drawLine(p1.x, p1.y, p2.x, p2.y);

}

}

private void processInput() {

// TODO Auto-generated method stub

keyboard.poll();

mouse.poll();

if (keyboard.keyDownOnce(KeyEvent.VK_SPACE)) {

System.out.println("VK_SPACE");

}

// if buffer is pressed for first time,

// start drawing lines

if (mouse.buttonDownOnce(MouseEvent.BUTTON1))

;

{

drawingLine = true;

}

// if the button is down,add line point

if (mouse.buttonDown(MouseEvent.BUTTON1)) {

lines.add(mouse.getPosition());

// if the button is not down but we were drawing,

// add a null to break up the lines

}

// if 'C' is down,clear the lines

if (keyboard.keyDownOnce(KeyEvent.VK_C)) {

lines.clear();

}

}

private void sleep(long sleep) {

// TODO Auto-generated method stub

try {

Thread.sleep(sleep);

} catch (InterruptedException ex) {

// TODO Auto-generated catch block

}

}

protected void onWindowClosing() {

try {

running = false;

gameThread.join();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.exit(0);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

final SimpleMouseExample app = new SimpleMouseExample();

app.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

app.onWindowClosing();

}

});

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

app.createAndShowGUI();

}

});

}

}

编译没有问题,运行后显示

Exception in thread "Thread-2" java.lang.NullPointerException

at javagames.input.SimpleMouseExample.renderFrame(SimpleMouseExample.java:73)

at javagames.input.SimpleMouseExample.gameLoop(SimpleMouseExample.java:63)

at javagames.input.SimpleMouseExample.run(SimpleMouseExample.java:56)

at java.lang.Thread.run(Unknown Source)补充:有大神解决一下吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值