Method同时返回String和null。

时间:2016-01-17 17:00:08

标签: java string null

我一个月以来一直在学习Java,并希望用GUI创建Hangman Game。我花了两天的时间试图找出最新情况,但是我自己找不到解决方案,也没有google的任何帮助。

我认为问题是(除了肯定的noobish方法:P)displayWord()方法同时返回String AND null,但不知道如何修复它以及为什么它给了我一个空...

如果有人如此善良并指出错误,我将非常感激。

import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game {
    private CLabel guessRemainLabel = new CLabel("GUESSES REMAINING: ");
    private CTextField guessRemainField = new CTextField("");
    private CLabel wordLabel = new CLabel("WORD TO GUESS: ");
    private CTextField wordField = new CTextField("");
    private CButton[] buttons = new CButton[26];
    private CButton guessButton = new CButton("GUESS");
    private String qwerty = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ButtonListener buttonListener = new ButtonListener();
    private char chosenChar;
    private String buttonText;

    private String wordDisplayed;
    private String wordPicked;
    private String wordChecked;
    private String word;
    private boolean buttonPressed = false;

    public Game() {
        // Frame settings
        JFrame frame = new JFrame("Hangman");
        frame.setSize(900, 600);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        MigLayout layout = new MigLayout("", // layout constrains
                "[] [] [] [] [] [] []", // columns constrains
                "[]40[]10[]30[] [] []"); // rows constrains
        JPanel panel = new JPanel();
        panel.setLayout(layout);

        frame.add(panel);

        // Remaining guesses
        panel.add(guessRemainLabel, "span 2");
        panel.add(guessRemainField, "span 7, alignleft, wrap");

        // Word
        panel.add(wordLabel, "span 7, center, wrap");
        wordField.setText(displayWord());
        panel.add(wordField, "span 7, center, wrap");

        // Buttons
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new CButton("" + qwerty.charAt(i));
            buttons[i].addActionListener(buttonListener);
            if (i == 6 || i == 13 || i == 20) {
                panel.add(buttons[i], "wrap");
            } else {
                panel.add(buttons[i]);
            }
        }
        panel.add(guessButton, "span");
        guessButton.setPreferredSize(new Dimension(224, 50));
        guessButton.setBackground(new Color(243, 156, 18));

        newGame();

        frame.pack();
        frame.setVisible(true);
    }

    // Buttons' actions
    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (CButton button : buttons) {
                if (e.getSource().equals(button)) {
                    buttonText = button.getText();
                    chosenChar = button.getText().charAt(0);
                    buttonPressed = true;
                    checkWord();
                }
            }
        }
    }


    public void newGame() {
        pickWord();
        checkWord();
        displayWord();
    }

    public String pickWord() {
        WordReader wordReader = new WordReader();
        wordPicked = wordReader.getRandomWord();
        word = wordPicked;
        return wordPicked;
    }


    public String checkWord() {
        if (!buttonPressed) {
            // replace all letters with dashes
            // word.replaceAll(".","_");
        } else {
            if (word.toUpperCase().contains(buttonText)) {
                for (int i = 0; i < word.length(); i++) {
                    if (word.toUpperCase().charAt(i) == chosenChar) {
                        // show letters if char found
                    }
                }
            } else {
                // count lives left 
            }
        }

        wordChecked = word;
        return wordChecked;
    }

    public String displayWord() {
        wordDisplayed = wordChecked;
        return wordDisplayed;
    }

    public static void main(String[] args) {
        // Anti-aliasing
        System.setProperty("awt.useSystemAAFontSettings", "on");
        System.setProperty("swing.aatext", "true");

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Game();
            }
        });
    }


}

2 个答案:

答案 0 :(得分:0)

这是找出问题的最简单方法。你说你的displayWord()方法同时返回String和null(虽然这不可能)。这意味着:

public String displayWord() {
    wordDisplayed = wordChecked;
    return wordDisplayed;
}

在上述方法中,wordChecked必须为null,因为wordDisplayed会变为null,因此该方法会返回null,从而产生问题。

我会通过你的代码来设置变量wordDisplayed的值,并确定你是否在设置它之前访问它,因为在设置私有实例变量之前访问会给你一个{ {1}}值。

编辑:这也是一个主要问题。您的大多数方法返回null,但是当您运行它们时,您还将返回值设置为变量。我建议您将返回类型设置为String,因为如果您不打算使用返回的值,则没有理由将返回类型设为void

答案 1 :(得分:0)

根据你所拥有的,我相信你的问题在你描述的方法中,displayWord()。在这种方法中,你根本不会初始化你的wordChecked变量,因为你使用它来设置wordDisplayed,所以可能会引发异常。