java - 构造函数中的 NullPointerException

标签 java swing user-interface nullpointerexception actionlistener

我已将问题范围缩小到 GUI 中的一行 Java 代码。

Shop1.addProduct(new Product(proid, proName, proPrice, proQis));

每次我单击附加的按钮时。它不是按钮,因为没有它也可以正常运行。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ShopFrame$AddListener.actionPerformed(ShopFrame.java:56)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(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$200(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)

我一生都无法解决这个问题。

最佳答案

Shop1 似乎为 null。尝试添加空检查:

if(Shop1 != null) Shop1.addProduct(new Product(proid, proName, proPrice, proQis));

另外,请注意,请遵循 Java 命名约定,并且不要以大写字母开头变量。使用 shop1,而不是 Shop1

关于java - 构造函数中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305948/

相关文章:

java - 如何使用 SimpleDateFormat 格式化日期

java - 当我尝试设置 clickListener 时,应用程序崩溃

Java 从 int 到 char 的转换表现得很奇怪

c++ - QObject::connect:没有此类信号-QT和OpenCV SIGNAL以及connect()错误

c# - 当主 UI 线程卡住时,为什么我的 PC 为 'freeze'?

java - 数组/方法/Java 帮助

Java JList remove() 方法抛出 ArrayOutOfBoundsException

java - 为什么 JDialog 中的键绑定(bind)不起作用?

java - GridLayout 中的 JTextField

java - Android 如何在 UI 上更新 textview