当word绝对不为空时,获取字符串方法返回null

时间:2016-10-23 14:21:53

标签: java string null

我正在编写一个刽子手游戏,由于某些原因,我的getW()方法在返回一个肯定不为空的字符串时返回null。我已经广泛添加了System.out.println以确保word保持不变。它似乎在所有地方都相同,但在getW()方法中,这就是getW()返回null的原因。

这是我的代码:

Hangman.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Hangman extends JPanel{
    private String word;

    public void setupLayout(JFrame window){
        window.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        HangmanArea panel1 = new HangmanArea();
        panel1.setBackground(Color.blue);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        window.add(panel1, c);

        GuessedLetters panel2 = new GuessedLetters();
        panel2.setBackground(Color.green);
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 1;
        c.gridy = 0;
        window.add(panel2, c);

        Letters panel3 = new Letters();
        panel3.setBackground(Color.black);
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        window.add(panel3, c);
        System.out.println("Before repaint: "+ word);
        panel1.callRepaint();
        panel2.callRepaint();
        panel3.callRepaint();
        System.out.println("After repaint: " + word);
    }

    public void startWindow(){
        JFrame window = new JFrame();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = (int) screenSize.getHeight();
        int windowHeight = (int) ((screenHeight / 4) * 3);
        window.setSize(windowHeight, windowHeight);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setupLayout(window);
        window.setVisible(true);
    }

    public void startGame(){
        Prep prepare = new Prep();
        word = prepare.findWord(true);
        System.out.println("Word found: " + word);
        startWindow();
        System.out.println("Word is unchanged: " + word);
    }

    public static void main(String[] args){
        Hangman start = new Hangman();
        start.startGame();
    }
    public String getW(){
        System.out.println("getW()'s word: " + word);    //At this point, word is null
        return word;
    }
}

Letters.java:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Letters extends JPanel {
    private String word = "aaa";
    public void callRepaint(){
        Hangman object = new Hangman();
        word = object.getW();
        System.out.println("Word for Letters: " + word);
        repaint();
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawString("letters", 50, 50);
    }
}

对不起,如果这看起来像很多代码,但Hangman.java和Letters.java是两个重要的类。 Prep.java的findWord()方法只是从单词列表中随机抽取一个单词并返回它。我认为发布信息并不是必要的,但我可以。除callRepaint()paintCompnent(Graphics g)方法外,GuessedLetters.java和HangmanArea.java完全为空。如果你需要,我可以发布它们,但我不认为它们非常重要。

输出可能如下所示:

Word found: night
Before repaint: night
getW()'s word: null
Word for Letters: null
After repaint: night
Word is unchanged: night

我不知道造成这种情况的原因。

2 个答案:

答案 0 :(得分:1)

问题在于,您没有将word变量初始化的对象(在您发布的示例输出中的#34; night")传递给callRepaint方法,而是在该方法中创建一个新实例(未连接到" main"对象)。相反,您应该将对象作为参数传入,例如:

public void callRepaint(Hangman object){
        word = object.getW();
        System.out.println("Word for Letters: " + word);
        repaint();
}

并在Hangman课程中调用它:

panel1.callRepaint(this);
panel2.callRepaint(this);
panel3.callRepaint(this);

或者可能只是传递String,例如:

public void callRepaint(String word){
        System.out.println("Word for Letters: " + word);
        repaint();
}

并以这种方式称呼它:

panel1.callRepaint(getW());
panel2.callRepaint(getW());
panel3.callRepaint(getW());

答案 1 :(得分:0)

在callRepaint中,您创建一个新的Hangman对象,其word成员永远不会被初始化。

要么声明你的单词变量是静态的,这意味着每个Hangman实例都会共享这个变量(它不再是一个类成员,这是一个糟糕的编码风格),或者在实例化时将原始的Hangman实例添加为参数。字母类或在调用callRepaint期间。或者在Letters instantiation期间将Hangman的单词成员设置为参数。