尝试将String追加到Java Applet Panel中的TextArea时出现NullPointerException

Ada*_*ort 0 java applet textarea awt layout-manager

我正在Java Applet中创建一个文本游戏,所以我可以在我的网站上显示它并让人们在那里播放它,但是我遇到了在TextArea中显示任何文本的问题.

这是我的主要课程:

package com.game.main;

import java.applet.*;
import java.awt.*;

public class Main extends Applet {

    private TextField commandInput;
    private TextArea messageDisplay;
    private Button button;
    public Message messages;

    // Initialisation method
    public void init() {
        super.init();

        // Define colours
        setBackground(Color.white);
        setForeground(Color.black);

        Panel appletPanel = new Panel();

        // Use a border layout
        BorderLayout b = new BorderLayout();
        appletPanel.setLayout(b);
        add(appletPanel);

        this.setSize(800, 400);

        // Define UI items
        commandInput = new TextField(20);
        messageDisplay = new TextArea(20, 60); // 20 rows x 60 chars
        button = new Button("Proceed");
        Panel inputPanel = new Panel();

        // Add components to our layout / panels
        inputPanel.add(commandInput);
        inputPanel.add(button);
        appletPanel.add("North", messageDisplay);
        appletPanel.add("South", inputPanel);

        messageDisplay.append(messages.getIntro());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Messages类(它包含所有消息,当用户点击按钮时,它将使用getWhateverMessage方法追加下一条消息:

package com.game.main;

public class Message {

    public String currentMessage;

    public String getCurrentMessage() {
        return currentMessage;
    }

    public void setCurrentMessage(String message) {
        currentMessage = message;
    }

    public String getIntro() {
        return "Welcome, This is a text adventure game created by me, Adam Short, as a little project to " +
               "exercise storytelling as well bring a retro style game to you, the player. To play this " +
               "game all you need is a keyboard to type your answers into the input box below. Keep your " +
               "answers relevant or you won't progress through the game at all. Type your answer into the " +
               "input box and hit the Proceed button or enter on your keyboard. Different answers will lead " +
               "to different scenearios and sequences of events. Be careful. Ready to go? Type go in the box " +
               "and hit Proceed!";
    }
}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 5

public Message messages;
...
messageDisplay.append(messages.getIntro());
Run Code Online (Sandbox Code Playgroud)

您定义的消息变量为null,但您从不创建Message类的实例.

您需要在代码中的某处:

messages = new Message();
Run Code Online (Sandbox Code Playgroud)

在使用变量之前定义变量或构造函数中的某个位置时,可以执行此操作.

appletPanel.add("North", messageDisplay);
appletPanel.add("South", inputPanel);
Run Code Online (Sandbox Code Playgroud)

而且,上面的代码是错误的.阅读API以获取add()方法.建议您使用:

appletPanel.add(messageDisplay, BorderLayout.NORTH);
appletPanel.add(inputPanel, BorderLayout.SOUTH);
Run Code Online (Sandbox Code Playgroud)