Nil*_*ger 4 java swing nullpointerexception jbutton
我目前正在开发一个用Java编写的游戏.为此我使用JFrames,JPanels和JButton.
要选择要播放的级别,您必须选择一个世界,然后选择一个级别.
启动游戏后,我WorldSelection
会创建一个新的实例,按下按钮后,所选的世界将被设置并LevelSelection
创建一个新的实例.
当按下按钮时,水平设置.
这是我的问题:
在Game
类中,该update
方法每秒检查选定的世界和级别60次,如果两者都选择了值,则会创建一个Stage
/ 的新实例Level
.
当我按下按钮设置时Level
,我得到一个NullPointerException
,即使它检查的值有一个值.
这不会一直发生; 它工作的一半时间(没有抛出异常),另外50%抛出异常.
Exception in thread "main" java.lang.NullPointerException
at Game.update(Game.java:41)
at Game.run(Game.java:23)
at Game.start(Game.java:34)
at Game.<init>(Game.java:10)
at Game.main(Game.java:77)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
游戏类:
public class Game {
private boolean running;
private WorldSelection ws;
private boolean chosen = false;
public Game() {
ws = new WorldSelection();
start();
}
public void run() { // game loop
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0;
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
update();
delta--;
}
if (System.currentTimeMillis() - timer == 1000) {
timer += 1000;
}
}
}
public void start() {
running = true;
run();
}
private void update() {
if (!chosen) {
if (ws.getWorld() == 1) {
if (ws.ls.getLevel() == 1) {
chosen = true;
//creates new stage here
}
if (ws.ls.getLevel() == 2) {
chosen = true;
//creates new stage here
}
if (ws.ls.getLevel() == 3) {
chosen = true;
//creates new stage here
}
}
else if (ws.getWorld() == 2) {
if (ws.ls.getLevel() == 1) {
chosen = true;
//creates new stage here
}
if (ws.ls.getLevel() == 2) {
chosen = true;
//creates new stage here
}
if (ws.ls.getLevel() == 3) {
chosen = true;
//creates new stage here
}
}
}
else {
//game updates here
}
}
public static void main(String[] args) {
Game game = new Game();
}
}
Run Code Online (Sandbox Code Playgroud)
WorldSelection类:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WorldSelection implements ActionListener {
public JFrame frame;
private int width, height;
private int world = 0;
private JButton button1;
public LevelSelection ls;
private JPanel panel;
public WorldSelection() {
this.width = 900;
this.height = 506;
frame = new JFrame();
frame.setSize(width, height);
frame.setResizable(false);
frame.setTitle("Quentins Adventure");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setBackground(Color.black);
button1 = new JButton("World 1");
button1.setBackground(Color.LIGHT_GRAY);
button1.setSize(120, 44);
button1.setLocation(80, 350);
button1.addActionListener(this);
panel.add(button1);
frame.add(panel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
setWorld(1);
ls = new LevelSelection(235,268);
frame.setVisible(false);
}
}
public int getWorld() {
return world;
}
public void setWorld(int world) {
this.world = world;
}
}
Run Code Online (Sandbox Code Playgroud)
LevelSelection类:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LevelSelection implements ActionListener {
public JFrame frame;
private int width, height, buttonSize = 50;
private int level = 0;
private JButton button1;
private Font font;
private JPanel panel;
public LevelSelection(int bPosX,int bPosY) {
this.width = 900;
this.height = 506;
frame = new JFrame();
frame.setSize(width, height);
frame.setResizable(false);
frame.setTitle("Quentins Adventure - Wold Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setBackground(Color.black);
button1 = new JButton("1");
button1.setBackground(Color.LIGHT_GRAY);
button1.setFont(font);
button1.setSize(buttonSize, buttonSize);
button1.setLocation(bPosX, bPosY);
button1.addActionListener(this);
panel.add(button1);
frame.add(panel);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
setLevel(1);
frame.dispose();
}
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
Run Code Online (Sandbox Code Playgroud)
你setWorld
然后做ls = new LevelSelection(235, 268);
这意味着,有时刻时ws.getWorld() == 1
是真的,但ws.ls
就是null
.
但这不仅是您的代码问题.您的代码不是线程安全的,这可能会导致很多错误,这些错误很难找到.
归档时间: |
|
查看次数: |
211 次 |
最近记录: |