简体   繁体   English

多线程程序中的java.lang.NullPointerException

[英]java.lang.NullPointerException in the multithread program

This is the code: 这是代码:

import java.applet.Applet;
import java.awt.Color;`
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

@SuppressWarnings("serial")
public class Pong extends Applet implements Runnable, KeyListener{

final int width = 700, height = 500;

public static int score = 0;

Thread thread;
HumanPaddle p1;
Ball b1;

public void init(){
    this.resize(width, height);

    this.addKeyListener(this);

    thread = new Thread(this);
    thread.start();

    p1 = new HumanPaddle(1);
    b1 = new Ball();
}

public void paint(Graphics g){
    g.setColor(Color.black);
    g.fillRect(0, 0, width, height);
    p1.draw(g);
    b1.draw(g);

    g.setColor(Color.red);
    g.drawString("Score: " + Integer.toString(score), width/2 - 20, 10);

}

public void update(Graphics g){
    paint(g);
}

public void run() {
    for(;;){

        p1.move();
        b1.move();

        b1.paddleCollision(p1, p1);  

        if(b1.getX() > width + 10){score++;
        }else if(b1.getX() < -10){score--;}

        repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

public void keyPressed(KeyEvent key) {
    if(key.getKeyCode() == KeyEvent.VK_UP || key.getKeyCode() == KeyEvent.VK_W){
        p1.setUpAccel(true);
    }else if(key.getKeyCode() == KeyEvent.VK_DOWN || key.getKeyCode() == KeyEvent.VK_S){
        p1.setDownAccel(true);
    }
}

public void keyReleased(KeyEvent key) {
    if(key.getKeyCode() == KeyEvent.VK_UP || key.getKeyCode() == KeyEvent.VK_W){
        p1.setUpAccel(false);
    }else if(key.getKeyCode() == KeyEvent.VK_DOWN || key.getKeyCode() == KeyEvent.VK_S){
        p1.setDownAccel(false);
    }
}

public void keyTyped(KeyEvent key) {

}

}

The thing is that SOMETIMES, only sometimes, the code fails and shows 关键是有时候,代码会失败并显示

"Exception in thread "Thread-3" java.lang.NullPointerException at Pong.run(Pong.java:48) at java.lang.Thread.run(Unknown Source)" “线程“ Thread-3中的异常”,java.lang.Thread.run(未知源)的Pong.run(Pong.java:48)处的java.lang.NullPointerException”

I know that means there's an error on line 48 Line 48: "p1.move();" 我知道这意味着第48行第48行有错误:“ p1.move();”

And makes non sense because it is a valid method and only fails sometimes... 并且没有意义,因为它是一种有效的方法,有时只会失败...

Please, I need quick help... Thank you in advance. 请,我需要快速帮助...预先感谢您。

You have a race condition. 您有比赛条件。 Because you start your thread in init() before p1 has been initialized, you run the risk that the thread might run before init has the chance to initialize it. 因为您在初始化p1之前在init()中启动了线程,所以冒着线程可能在init有机会对其进行初始化之前运行的风险。 You only need move the initialization of p1 and b1 to before the thread start: 您只需要在线程启动之前将p1和b1的初始化移到:

...
    p1 = new HumanPaddle(1);
    b1 = new Ball();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM