2014-12-19 39 views
0

我正在製作一個包含1個TextArea,1個Label和1個TextField的Java Swing應用程序。Swing apllication在ActionEvent上拋出java.lang.NullPointerException

我的代碼如下:當按下回車鍵

package mainpack.newboston; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Insets; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class Panel extends JPanel implements ActionListener { 
    protected JTextArea textarea123; 
    protected JTextField textfield1; 
    JLabel label; 
    JFrame frame; 

    public Panel() { 
     setLayout(null); 

     JTextArea textarea123 = new JTextArea(); 
     JTextField textfield1 = new JTextField(30); 
     JLabel label = new JLabel("Command: "); 

     add(textarea123); 
     add(textfield1); 
     add(label); 

     // textarea123.append("Hello"); 
     textarea123.setEditable(false); 
     textarea123.setSize(600, 600); 
     textarea123.setBackground(Color.BLACK); 
     textarea123.setForeground(Color.WHITE); 

     textfield1.setBorder(null); 
     textfield1.setBackground(Color.BLACK); 
     textfield1.setForeground(Color.white); 

     textfield1.addActionListener(this); 
     textfield1.setActionCommand("commandexecuted"); 

     // label.setForeground(Color.WHITE); 
     label.setBackground(Color.ORANGE); 

     //####################################################### 
     Insets insets = getInsets(); 

     Dimension fieldsize = textfield1.getPreferredSize(); 
     textfield1.setBounds(63 + insets.left, 555 + insets.top, 537, fieldsize.height); 

     textfield1.setFocusable(true); 
     textfield1.setEnabled(true); 
     Dimension labelsize = label.getPreferredSize(); 
     label.setBounds(0 + insets.left, 555 + insets.top, labelsize.width, labelsize.height); 

     @SuppressWarnings("unused") 
     Dimension size = textarea123.getPreferredSize(); 
     textarea123.setBounds(0 + insets.left, 0 + insets.top, 600, 555); 
     //########################################################## 
    } 

    /** 
     * Create the GUI and show it. For thread safety, 
     * this method should be invoked from the 
     * event-dispatching thread. 
     */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("New Boston"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Set up the content pane. 
     frame.add(new Panel()); 
     //Size and display the window. 
     Insets insets = frame.getInsets(); 
     frame.setResizable(false); 
     frame.setSize(600 + insets.left + insets.right, 600 + insets.top + insets.bottom); 
     frame.setVisible(true); 
    } 

    public void ConsoleLog(String message){ 
     System.out.println(message); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    public void textappend(String message) { 
     textarea123.append(message); 
    } 

    public void actionPerformed(ActionEvent evt) { 
     if (evt.getActionCommand().equals("commandexecuted")) { 
      if (textfield1.getText().equals("Try")) { 
       ConsoleLog("Hello"); 
      } 
     } 
    } 
} 

錯誤信息,而文本字段是重點:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at mainpack.newboston.Panel.actionPerformed(Panel.java:127) 
    at javax.swing.JTextField.fireActionPerformed(Unknown Source) 
    at javax.swing.JTextField.postActionEvent(Unknown Source) 
    at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source) 
    at javax.swing.SwingUtilities.notifyAction(Unknown Source) 
    at javax.swing.JComponent.processKeyBinding(Unknown Source) 
    at javax.swing.JComponent.processKeyBindings(Unknown Source) 
    at javax.swing.JComponent.processKeyEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$400(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

我想使它看起來像一個控制檯,並使用一些命令,但我會稍後做,因爲我不能使用ActionEvents ...

回答

2

在您的構造函數中,更改此行

JTextField textfield1 = new JTextField(30); 

this.textfield1 = new JTextField(30); 
+0

真的,那麼容易嗎?我試圖修復這個小時......很多謝謝:)) – BGdeveloper

+0

爲您的文本區域和標籤做同樣的事情。 – Kapparino

1

您正在定義字段,例如protected JTextField textfield1;

然後,您將定義局部變量,如JTextField textfield1 = new JTextField(30);

因此,例如,textfield1是您的對象(在本地環境中),並且this.textfield1爲空,因爲您從不給它賦值。

我建議指定一個變量來你的領域,像這樣:

textfield1 = new JTextField(30);

嘗試這種技術與你的領域,這既避免了分配相同的名字的局部變量,並且也避免了編寫一個隱含this ,分配值時。

我也建議看看http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

相關問題