我正在研究java中的游戏,并创建了一个JTextArea来显示大部分文本,以及一个textPrinter方法来将文本追加到JTextArea。但是每当我尝试调用textPrinter时,我都会得到一个空指针异常。我知道传入方法的字符串不是null,因为从textPrinter方法运行System.out.println(text)时它表明值已正确传递。
我的主要课程的相关代码:
public class stickGame {
public JTextArea gameText;
public JPanel gamePanel;
public JFrame gameWindow;
public static void main(String[] args) {
stickGame game = new stickGame();
TextHandler text = new TextHandler();
gameEnabled = true;
while (gameEnabled == true) {
MainMenu menu = new MainMenu();
victory = false;
//SinglePlayer Logic
if (gameMode == 1) {
gameLevel = 0;
while (defeat != true) {
playerHealth = 100;
aiHealth = 100 + (25 * gameLevel);
pRage = 0;
aiRage = 0 + (10 * gameLevel);
secondPRage = 0;
secondPlayerHealth = 100;
text.textPrinter("Level "+ (gameLevel + 1)+"\n");
}
}
}
}
我的TextHandler类代码:
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextHandler extends stickGame {
private String pText;
public void WindowHandler() {
gameWindow = new JFrame("Stick Game");
gameWindow.setVisible(true);
gameWindow.setSize(1000, 600);
gamePanel = new JPanel();
gamePanel.setMaximumSize(new Dimension(200, 120));
gamePanel.setVisible(true);
gameText = new JTextArea();
gameText.setSize(600, 600);
gameText.setLineWrap(true);
gameText.setVisible(true);
gameText.setFont(new Font("monospaced", Font.PLAIN, 12));
JButton attackB = new JButton("Attack");
JButton blockB = new JButton("Block");
JButton rushB = new JButton("Rush");
JButton healB = new JButton("Heal");
JButton ultimateB = new JButton("Ultimate");
JButton cheatB = new JButton("Cheat");
attackB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveChoice = "attack";
duplicateCheck = true;
}
});
blockB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveChoice = "block";
duplicateCheck = true;
}
});
rushB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveChoice = "rush";
duplicateCheck = true;
}
});
healB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveChoice = "heal";
duplicateCheck = true;
}
});
ultimateB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveChoice = "ultimate";
duplicateCheck = true;
}
});
cheatB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pRage = stickGame.pRage + 100;
}
});
gamePanel.add(gameText);
gamePanel.add(attackB);
gamePanel.add(blockB);
gamePanel.add(rushB);
gamePanel.add(healB);
gamePanel.add(ultimateB);
gamePanel.add(cheatB);
gameWindow.add(gamePanel);
gameWindow.repaint();
}
public void textPrinter(String text) {
System.out.println(text);
gameText.append(text);
}
}
这是错误:
Exception in thread "main" java.lang.NullPointerException
at TextHandler.textPrinter(TextHandler.java:113)
at stickGame.main(stickGame.java:431)
空指针异常发生在text.textPrinter(" Level" +(gameLevel + 1)+" \ n");在主要方法。究竟是什么问题,我应该如何解决它?
答案 0 :(得分:0)
您的JTextArea是在WindowHandler方法中构造的,永远不会被调用。在构造TextHandler之后调用WindowHandler()或从TextHandler的构造函数中调用它