谁能告诉我为什么下面的代码会抛出空指针异常?异常在行 numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField)); 在 InputJFrame1.java 类中。
import java.awt.*;
public class InputJFrame1 extends javax.swing.JFrame
{
private javax.swing.JTextField numberJTextField;
public InputJFrame1()
{
numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField));
}
private void initComponents()
{
numberJTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(numberJTextField);
}
public static void main(String args[])
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(InputJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InputJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InputJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InputJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new InputJFrame1().setVisible(true);
}
});
}
}
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
public class MyMouseAdapter extends MouseAdapter
{
private JTextField jTextField;
MyMouseAdapter(JTextField jTextField)
{
this.jTextField=jTextField;
}
@Override
public void mouseClicked(MouseEvent e)
{
jTextField.setForeground(Color.red);
}
}
堆栈跟踪:
java.awt.event.InvocationEvent 的 javaapplication8.InputJFrame1$1.run(InputJFrame1.java:73) 的 javaapplication8.InputJFrame1.(InputJFrame1.java:9) 的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常。 dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue .java:688) 在 java.awt.EventQueue$3.run(EventQueue.java:686) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 中的 java.awt.EventQueue.dispatchEvent(EventQueue.java:697)。EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread .java:138) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:91)