尝试将字符串附加到Java小程序面板中的文本区域时出现NullPointerException
我在一个Java小程序中创建了一个文本游戏,这样我就可以在我的网站上显示它,让人们在那里玩它,但是我在获取文本区域中显示的任何文本时遇到了问题 这是我的主要课程:尝试将字符串附加到Java小程序面板中的文本区域时出现NullPointerException,java,applet,textarea,awt,layout-manager,Java,Applet,Textarea,Awt,Layout Manager,我在一个Java小程序中创建了一个文本游戏,这样我就可以在我的网站上显示它,让人们在那里玩它,但是我在获取文本区域中显示的任何文本时遇到了问题 这是我的主要课程: package com.game.main; import java.applet.*; import java.awt.*; public class Main extends Applet { private TextField commandInput; private TextArea messageDi
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());
}
}
这是我的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!";
}
}
您定义的messages变量为null,但从未创建Message类的实例
在代码中的某个地方,您需要:
messages = new Message();
可以在定义变量时执行此操作,也可以在使用变量之前在构造函数中的某个位置执行此操作
appletPanel.add("North", messageDisplay);
appletPanel.add("South", inputPanel);
而且,上面的代码是错误的。阅读add方法的API。建议您使用:
appletPanel.add(messageDisplay, BorderLayout.NORTH);
appletPanel.add(inputPanel, BorderLayout.SOUTH);
camickr是对的,您需要创建Message类的对象,变量messages需要保存对它的引用: 消息=新消息
然后,只有您可以访问其实例变量和方法。Stacktrace请将该行指向我们……堆栈跟踪中的错误会准确地告诉您问题所在。消息为空。1用于部署Java桌面应用程序。最好的选择通常是安装应用程序。使用。JWS在Windows、OSX和*nix上工作。2为什么AWT而不是Swing?关于放弃使用AWT组件的许多好理由,请参见上的回答。如果您需要支持旧的基于AWT的API,请参阅。非常感谢。我觉得错过创建消息实例太傻了。
appletPanel.add(messageDisplay, BorderLayout.NORTH);
appletPanel.add(inputPanel, BorderLayout.SOUTH);