这是刽子手。当我运行代码时,我得到几个空指针异常,并且不知道如何解决这个问题。
这就是编译错误所说的:
Exception in thread "main"
java.lang.NullPointerException at
hangmanrevised.createGame(hangmanrevised.java:66)
at
hangmanrevised.<init>(hangmanrevised.java:35)
at
hangmanrevised.main(hangmanrevised.java:202)
感谢任何帮助。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.lang.reflect.Array;
import javax.swing.*;
public class hangmanrevised extends JFrame implements ActionListener {
private int wrongs;
private String right;
private StringBuffer guess;
private TextField Letter;
private String message;
private String information;
private JButton button;
private final int hung = 11;
public hangmanrevised() {
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
ImageIcon background = new ImageIcon("nightsky.png");
TextField Letter = new TextField();
JLabel label = new JLabel("pick a Letter");
Button button = new Button("Enter");
add(label);
add(button);
add(Letter);
button.addActionListener(this);
createGame();
}
public void createGame(){
wrongs = 0;
String word = "school|java|programming|science|computer";
String[] temp;
String delimiter = "\\|";
temp = word.split(delimiter);
Random randomPicker = new Random();
int randomWord = randomPicker.nextInt(temp.length);
right = new String(temp[randomWord]);
char positions[] = new char[right.length()];
for (int i = 0; i < right.length(); i++) {
positions[i] = '-';
}
String s = new String(positions);
guess = new StringBuffer(s);
Letter.setText("");
message="";
information = "";
repaint();
}
public void paint(Graphics g) {
super.paint(g);
//g.drawImage(background, 0, 156, Color.green, Enter);
int baseY = 350;
g.setColor(Color.lightGray);
g.drawRect(0, baseY,400,baseY);
g.fillRect(0, baseY,400,baseY);
g.setColor(Color.darkGray);
//g.drawRect(250,50,125,50);
//g.fillRect(250,50,125,50);
if (wrongs > 0){
g.drawLine(125,baseY,125,baseY-100);
g.drawLine(123,baseY,123,baseY-100);
}
if (wrongs > 1){
g.drawLine(110,baseY,125,baseY-15);
g.drawLine(108,baseY,122,baseY-15);
}
if (wrongs > 2){
g.drawLine(140,baseY,125,baseY-15);
g.drawLine(138,baseY,122,baseY-15);
}
if (wrongs > 3){
g.drawLine(125,baseY-100,175,baseY-100);
g.drawLine(125,baseY-98,175,baseY-98);
}
if (wrongs > 4){
g.drawLine(175,baseY-100,175,baseY-75);
}
if (wrongs > 5){
g.drawOval(170,baseY-75,10,12);
}
if (wrongs > 6){
g.drawOval(160,baseY-65,15,25);
g.fillOval(160,baseY-65,15,25);
}
if (wrongs > 7){
g.drawLine(150,baseY-65,170,baseY-60);
}
if (wrongs > 8){
g.drawLine(173,baseY-60,193,baseY-65);
}
if (wrongs > 9){
g.drawLine(155,baseY-30,170,baseY-45);
}
if (wrongs > 10){
g.drawLine(173,baseY-45,193,baseY-30);
}
g.drawString( message, 60, baseY+25 );
g.drawString( information, 25, baseY+45 );
g.drawString( new String (guess), 110, 60);
//listener();
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == button){
processTurn();
Letter.setText("");
repaint();
}
}
private void processTurn(){
String s, t;
char a;
s = Letter.getText();
a = s.charAt(0);
if (!Character.isLetter(a)){
message="Letters only";
return;
}
if (s.length()>1){
message="Enter 1 letter only";
return;
}
t = new String(guess);
if (t.indexOf(s) != -1){
JOptionPane.showMessageDialog(null, "LETTER USED ALREADY");
return;
}
if (right.indexOf(s) == -1){
message="";
wrongs++;
if (wrongs == hung){
JOptionPane.showMessageDialog(null, "YOU LOST!");
}
return;
}
for (int i = 0; i < right.length(); i++){
if (right.charAt(i) == a){
guess.setCharAt(i, a);
}
}
t = new String(guess);
if (t.indexOf('-') == -1){
JOptionPane.showMessageDialog(null, "YOU WIN!");
message="You win!";
return;
}
message="";
repaint();
}
public static void main(String[] args) {
hangmanrevised f = new hangmanrevised();
f.setVisible(true);
}
}
答案 0 :(得分:6)
在构造函数中,您不小心将new TextField()
分配给局部变量而不是字段。
更改
TextField Letter = new TextField();
到
Letter = new TextField();
letter
代替Letter
和HangmanRevised
代替hangmanrevised
答案 1 :(得分:1)
尝试使用“|”为你的分隔符
看看BalusC的评论。能够以简单的形式提出问题并运行调试器非常重要。