任何人都可以告诉我为什么以下代码抛出空指针异常?线路上抛出异常 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);
}
}
堆栈跟踪:
线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException 在javaapplication8.InputJFrame1。(InputJFrame1.java:9)at javaapplication8.InputJFrame1 $ 1.run(InputJFrame1.java:73)at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)at at java.awt.EventQueue.access $ 200(EventQueue.java:103)at java.awt.EventQueue $ 3.run(EventQueue.java:688)at java.awt.EventQueue $ 3.run(EventQueue.java:686)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:697)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:4)
不要从构造函数中删除initComponents
。它确保在使用它们之前正确初始化所有组件,因此它应该是构造函数的第一行。
public InputJFrame1()
{
initComponents();
numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField));
}
答案 1 :(得分:2)
我认为您忘记在默认构造函数上初始化numberJTextField
(和其他组件)
public InputJFrame1()
{
initComponents();
numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField));
}
答案 2 :(得分:2)
在调用构造函数时尚未分配字段numberJTextField
。在添加MouseListener之前,尝试在构造函数中调用initComponents
方法。
public class InputJFrame1 extends javax.swing.JFrame
{
private javax.swing.JTextField numberJTextField;
public InputJFrame1()
{
initComponents(); //Init components before using textfield
numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField));
}
private void initComponents()
{
numberJTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(numberJTextField);
}
答案 3 :(得分:1)
public InputJFrame1() {
numberJTextField.addMouseListener(new MyMouseAdapter(numberJTextField));
}
numberJTextField
为空,这就是原因。
答案 4 :(得分:0)
您正在实例化一个新的InputJFrame1
,但是没有调用您的初始化方法,这意味着numberJTextField
为空。
我会将您的初始化代码放在构造函数中。然后,您可以将相应的字段标记为final
,编译器将告诉您是否忘记初始化它们。
private final javax.swing.JTextField numberJTextField;
使用final
是避免将来出现此类初始化问题的好方法。