简体   繁体   中英

Need Help fixing NullPointerException

This is hangman. When I run the code I get a couple of null pointer exceptions and have no idea how to fix this.

This is what the compile error says exactly:

 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)

Any help is appreciated.

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);

    }
}

In the constructor, you're accidentally assigning new TextField() to a local variable instead of the field.

Change

TextField Letter = new TextField();

to

Letter = new TextField();

BTW, I strongly suggest you start your variables with lower case and class names with upper case, ie,

  • letter instead of Letter and
  • HangmanRevised instead of hangmanrevised

Try just using "|" for your delimiter
Look at BalusC comment. It is important to be able to put the problem in a simple form and run a debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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