简体   繁体   English

错误:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException

[英]ERROR : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

i faced a problem with null , i implement the same code before and it work well but after i add other codes its not work .. 我遇到了null的问题,我之前实现了相同的代码,因此效果很好,但是在添加其他代码后,效果不佳..

this is part of error : 这是错误的一部分:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at manager.flyer$13.mouseClicked(flyer.java:751)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)

and my code part which show that error : 和我的代码部分显示该错误:

JButton btnNewButton_4 = new JButton("Add All");
           btnNewButton_4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            int columnContainingPlanets =1 ;
            for (int index =0 ; index < tableModel.getRowCount(); index ++){
            if (tableModel.getValueAt(index, columnContainingPlanets).equals("")) {
            JOptionPane.showMessageDialog(panel_4, "Please Enter Discount Amount For All Items");

            return; }}

i try to replace columnContainingPlanets=1 to columnContainingPlanets=0 but also there is error : java.lang.NullPointerException 我尝试将columnContainingPlanets=1替换为columnContainingPlanets=0但是也出现错误: java.lang.NullPointerException

i will happy if any one know the solution because that error appeared suddenly, and it was work well .. 如果有人知道解决方案,我将很高兴,因为该错误突然出现了,并且效果很好。

We can't debug a NullPointerException for you. 我们无法为您调试NullPointerException。 You are the only person that has access to your code and the data in your application. 您是唯一有权访问您的代码和应用程​​序中的数据的人。 So you are the only person that can solve the problem. 因此,您是唯一可以解决问题的人。

A few pointers to help with debugging: 一些有助于调试的指针:

  1. Why are you creating a variable (columnContainingPlanets) to use as an index in the getValueAt(...) method. 为什么要创建一个变量(columnContainingPlanets)用作getValueAt(...)方法中的索引。 It won't cause a problem but it seems a bit unnecessary. 不会造成问题,但似乎没有必要。

  2. Don't code multiple statements in the same line of code. 不要在同一行代码中编写多个语句。 It makes it harder to determine which variable is null. 这使得确定哪个变量为null变得更加困难。 In your case I can see that "tableModel" could be null, or the value returned from the TableModel could be null. 在您的情况下,我可以看到“ tableModel”可以为null,或者从TableModel返回的值可以为null。 Until you know which value is null you can't solve the problem. 除非您知道哪个值为空,否则您将无法解决问题。

  3. You can't test for the String "" in the if statement. 您不能在if语句中测试字符串“”。 The getValueAt() method returns an Object. getValueAt()方法返回一个Object。 An Object is not a String. 对象不是字符串。

So you might restructure you code something like this: 因此,您可以重组代码,如下所示:

for (int index =0 ; index < tableModel.getRowCount(); index ++)
{
    Object value = tableModel.getValueAt(index, 1);

    if (value.toString().equals("")) 
    {
        JOptionPane.showMessageDialog(panel_4, "Please Enter Discount Amount For All Items");

        return; 
    }
}

Now the line number given in the NullPointerException will tell you exactly which variable is null. 现在,在NullPointerException中给出的行号将告诉您确切哪个变量为null。 Either: 要么:

  1. the "tableModel" is null (in which cause you have a problem with how you reference the table model, or “ tableModel”为null(导致您在引用表格模型时遇到问题,或者

  2. the "value" is null (in which case you have a problem with the data contained in your table model. “值”为null(在这种情况下,表模型中包含的数据有问题。

Finally, Don't use a MouseListener on a JButton . 最后,不要在JButton上使用MouseListener Instead you should be adding an ActionListener to the JButton . 相反,您应该将ActionListener添加到JButton Read the section from the Swing tutorial on How to Use Buttons . 阅读Swing教程中有关如何使用按钮的部分 Download the demo code and play with the examples. 下载演示代码并使用示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java-线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java 线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException? Java的 - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException? Java 线程““ AWT-EventQueue-0””中的Java异常java.lang.NullPointerException - Java Exception in thread '“AWT-EventQueue-0”' java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException eclipse:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - eclipse : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException For循环 - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException For Loops 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 结果集中的线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException in Resultset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM