[英]NullPointerException when using KeyListener to use setText
This is a word guessing game. 这是一个猜词游戏。 The first letter is shown everytime and they rest is not. 每次都会显示第一个字母,而其余的则不会显示。 Whenever a user types I want it to change the letters on the board, here is a screenshot: http://puu.sh/7moQS.jpg 每当用户键入我想要它更改板上的字母时,以下是屏幕截图: http : //puu.sh/7moQS.jpg
Error: 错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at BoardUI.keyTyped(BoardUI.java:48)
where line BoardUI.java:48
is 行BoardUI.java:48
在哪里
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(typed + "");
Game.java Game.java
public class Game {
}
testGUI.java testGUI.java
public class testGUI {
public static void main(String[] args) {
new GUI("Lingo");
}
}
BoardUI.java BoardUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class BoardUI extends JPanel implements KeyListener {
private final Game game;
private Board b = new Board();
private Color hoverColor = new Color(100, 100, 255);
public BoardUI(LayoutManager layout, Game game, String word) {
super(layout);
this.b = new Board();
this.game = game;
initComponents(word);
}
public BoardUI(Game game) {
this.game = game;
}
private void initComponents(String word) {
this.setLayout(new GridLayout(5, 5, 2, 2));
for (int i = 0; i < 5 * 5; i++) {
b.getLetters()[i] = new Label();
b.getLetters()[i].setBackground(Color.WHITE);
b.getLetters()[i].setForeground(Color.BLACK);
b.getLetters()[i].setAlignment(Label.CENTER);
b.getLetters()[i].setFont(new Font("Big", Font.BOLD, 48));
this.add(b.getLetters()[i]);
}
for (int i = 0; i < 5; i++) {
b.getLetters()[i].setText(word.charAt(i) + "");
b.getLetters()[i].setBackground(Color.BLUE);
}
b.setCurrentColumn(0);
b.setCurrentRow(0);
}
@Override
public void keyTyped(KeyEvent e) {
char typed = e.getKeyChar();
if (Character.isLetter(typed) && b.getCurrentColumn() < 5) {
typed = Character.toUpperCase(typed);
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(typed + "");
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);
if (b.getCurrentColumn() == 0) {
for (int i = 1; i < 5; i++) {
b.getLetters()[b.position(b.getCurrentRow(), i)].setText(".");
b.getLetters()[b.position(b.getCurrentRow(), i)].setBackground(Color.BLUE);
}
b.nextColumn(true);
if (b.getCurrentColumn() < 5) {
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(hoverColor);
}
if (typed == 8 && b.getCurrentColumn() >= 0) {
this.oneBack();
}
}
}
}
private void oneBack() {
if (b.getCurrentColumn() < 5) {
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);
b.nextColumn(false);
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(".");
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
Board.java: Board.java:
import java.awt.*;
public class Board {
private Reader reader = new Reader();
private Label[] letters = new Label[5 * 5];
private String[] words = new String[100];
private int currentRow, currentColumn;
public Label[] getLetters() {
return letters;
}
public String firstWord() {
return getWord().charAt(0) + "....";
}
public String getWord() {
int count = reader.LeesWoordenVanFile(words);
int x = (int) (Math.random() * 100);
int gokX = (x % count);
return words[gokX].toUpperCase();
}
public int position(int row, int column) {
return row * 5 + column;
}
public void nextColumn(boolean next) {
if (next) {
currentColumn++;
} else {
currentColumn--;
}
}
public int getCurrentRow() {
return currentRow;
}
public int getCurrentColumn() {
return currentColumn;
}
public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}
public void setCurrentColumn(int currentColumn) {
this.currentColumn = currentColumn;
}
}
GUI.java: GUI.java:
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame {
private JPanel gamePanel;
private Game game;
private Board board = new Board();
private BoardUI boardUI = new BoardUI(game);
public GUI(String title) throws HeadlessException {
super(title);
game = new Game();
setupVieuw();
showFrame();
this.addKeyListener(boardUI);
}
public void setupVieuw() {
getContentPane().removeAll();
initComponents(board.firstWord());
layoutComponents();
this.validate();
}
public void layoutComponents() {
getContentPane().add(gamePanel, BorderLayout.CENTER);
}
private void initComponents(String word) {
gamePanel = new JPanel(new BorderLayout());
gamePanel.add(new BoardUI(new BorderLayout(),game, word));
}
private void showFrame() {
this.setBounds(50, 50, 800, 800);
this.setPreferredSize(new Dimension(600, 600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
this.doLayout();
}
}
This is really confusing because you've got some redundant code and are doing things twice in some cases, but here's what I've found. 这确实很令人困惑,因为您有一些冗余代码,并且在某些情况下执行两次操作,但这就是我的发现。
In GUI.java, you're instantiating BoardUI
here: 在GUI.java中,您要在此处实例化BoardUI
:
private Game game;
private Board board = new Board();
private BoardUI boardUI = new BoardUI(game);
Notice that, 请注意,
BoardUI
with a null Game
object 您正在使用null Game
对象初始化BoardUI
You're calling this BoardUI
constructor: 您正在调用此BoardUI
构造函数:
public BoardUI(Game game) { this.game = game; }
and not this constructor - aka. 而不是这个构造函数-又名。 the one that actually fills your letters
array with new Label()
objects: 一个实际上用new Label()
对象填充您的letters
数组的对象:
public BoardUI(LayoutManager layout, Game game, String word) { super(layout); this.b = new Board(); this.game = game; initComponents(word); }
Still in GUI.java
, you do eventually call a different initComponents()
, but you call it with a new BoardUI
instance 仍然在GUI.java
,您最终确实调用了另一个initComponents()
,但是使用新的BoardUI
实例调用了它。
private void initComponents(String word) {
gamePanel = new JPanel(new BorderLayout());
gamePanel.add(new BoardUI(new BorderLayout(),game, word));
}
Right after that returns, you're back on the last line of the GUI
class constructor and you set the key listener to the BoardUI
that hasn't run initComponents()
: 返回之后,立即返回GUI
类构造函数的最后一行,并将键侦听器设置为未运行initComponents()
的BoardUI
:
this.addKeyListener(boardUI);
TL;DR: You're adding a key listener to a BoardUI
object that hasn't run initComponents()
TL; DR:您正在向未运行initComponents()
的BoardUI
对象添加键侦听器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.