Java JTextArea在Swing中出现问题

Java JTextArea在Swing中出现问题,java,swing,textarea,nullpointerexception,Java,Swing,Textarea,Nullpointerexception,我在更新文本区域时遇到问题 我在gui.java中声明textArea: JTextArea textArea; 我启动GUI public void startGUI() { // These are all essential GUI pieces JLabel jLabInstruction, jLaberror; JLabel copyright = new JLabel(""); JTextField uI = new

我在更新文本区域时遇到问题

我在
gui.java
中声明
textArea

JTextArea textArea;
我启动GUI

public void startGUI() {
        // These are all essential GUI pieces
        JLabel jLabInstruction, jLaberror;
        JLabel copyright = new JLabel("");
        JTextField uI = new JTextField("");
        JTextArea textArea = new JTextArea("");
        JButton jbtnSubmit;

        final JFrame jfrm = new JFrame("app name!");
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(300, 300);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        jLabInstruction = new JLabel("SYSTEM: Please type in a command: ");
        jbtnSubmit = new JButton("Submit");
        jLaberror = new JLabel("");
        textArea.setMargin(new Insets(10,10,10,10));

        jfrm.add(jLaberror);
        jfrm.add(textArea);
        jfrm.add(jLabInstruction);
        jfrm.add(uI);
        jfrm.add(jbtnSubmit);
        jfrm.add(new JSeparator(SwingConstants.HORIZONTAL));
        jfrm.add(copyright);
        jfrm.setVisible(true);
    }
我有一个方法可以写入上面的
textArea

public void writeToTextArea(String userInputText) {
        textArea.append("\nSYSTEM: "
                + userInputText);
    }
另外,在
tasks.java
中,我可以调用最后一种方法:

gui.writeToTextArea("PROGRAM STARTED!");
我的问题是文本区域字段没有更新。没有输入任何内容。我认为这是因为它找不到
textArea
是什么。我得到一个:

Exception in thread "main" java.lang.NullPointerException 

您正在
startGUI
函数中声明另一个名为
textArea
的变量,该变量隐藏类级别
textArea
。这就是为什么当您稍后在程序中尝试写入文本区域时会得到NPE

JTextArea textArea;

public void startGUI() {
    JLabel jLabInstruction, jLaberror;
    JLabel copyright = new JLabel("");
    JTextField uI = new JTextField("");
    JTextArea textArea = new JTextArea(""); //<-- Your hiding your class variable here

    // ... rest of your code
}
JTextArea textArea;
public void startGUI(){
JLabel jLabInstruction,jlaberor;
JLabel版权=新JLabel(“”);
JTextField uI=新的JTextField(“”);

JTextArea textArea=new JTextArea(“”;//您正在
startGUI
函数中声明另一个名为
textArea
的变量,该变量隐藏了类级别
textArea
。这就是为什么您稍后在程序中尝试写入文本区域时会得到NPE

JTextArea textArea;

public void startGUI() {
    JLabel jLabInstruction, jLaberror;
    JLabel copyright = new JLabel("");
    JTextField uI = new JTextField("");
    JTextArea textArea = new JTextArea(""); //<-- Your hiding your class variable here

    // ... rest of your code
}
JTextArea textArea;
public void startGUI(){
JLabel jLabInstruction,jlaberor;
JLabel版权=新JLabel(“”);
JTextField uI=新的JTextField(“”);

JTextArea textArea=new JTextArea(“”;//我试图注释掉这一行,但它似乎不起作用。不要注释掉这一行。停止重新声明变量即可。Aka
textArea=new JTextArea(“”)
+1.@droidus,由于需要创建文本区域,您无法注释掉该行。我收到了更多错误:userInput.askGetInput(“创建文件时出错。是否继续诊断?(键入“y”或“是”或“n”或“否”),这只需要一分钟。”;(taskBckg.java),以及此处…//写入文本区域public void writeToTextArea(String userInputText){textArea.append(“\n系统:“+userInputText”);}//要求用户输入public void askGetInput(String outText){writeToTextArea(outText);}我仍然得到“java.lang.NullPointerException”错误,不确定原因。只有在添加这3条语句后才会发生……我尝试注释掉这一行,但似乎不起作用。不要注释掉这一行。只需停止重新说明变量。Aka
textArea=new JTextArea(“”)
+1.@droidus,由于需要创建文本区域,您无法注释掉该行。我收到了更多错误:userInput.askGetInput(“创建文件时出错。是否继续诊断?(键入“y”或“是”或“n”或“否”),这只需要一分钟。”;(taskBckg.java),以及此处…//写入文本区域public void writeToTextArea(String userInputText){textArea.append(“\n系统:“+userInputText”);}//要求用户输入public void askGetInput(String outText){writeToTextArea(outText);}我仍然得到“java.lang.NullPointerException”错误,我不知道为什么。只有在添加这3条语句后才会发生。要更快地获得更好的帮助,请发布一条。要更快地获得更好的帮助,请发布一条。