在线程“AWT-EventQueue-0”中获取异常java.lang.NullPointerException错误

时间:2017-12-16 03:38:44

标签: java

所以在我的程序中我有一个带有inputdialog的JOptionPane工作正常,但每当我点击“取消”时,它就会给我这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AccountingJournal.actionPerformed(AccountingJournal.java:341)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

这是错误的地方:

public class AccountingJournal implements ActionListener {
    JFrame frame, frame2, frame3, frame4;
    JLabel main_title, test, title, date, accountNumber, description, 
creditOrDebit, amount, dollarSign, date2;
    JButton main_addTransaction, main_addAccount, main_reportAccount, 
main_reportCreditDebit, main_reportFull, main_Exit, addTransaction_confirm, 
addTransaction_cancel;
    String [] accountNumbers = new String[100];
    JComboBox dateDay, dateMonth, dateYear, accountNumberField, creditDebit;
    JTextField descriptionField, amountMoney;
    File f;
    FileReader r;
    BufferedReader b = null;
    FileWriter fw;
    BufferedWriter bw = null;
    String whichReport = "";
    String accountNum = "";
    String whichAccount = "";


    if (evt.getSource()==main_reportCreditDebit){
        String [] creditDebit = {"Credit", "Debit"};
        String reportCreditDebit = (JOptionPane.showInputDialog(null, "Select Credit or Debit", "Report by Credit/Debit",
                                    JOptionPane.PLAIN_MESSAGE, null, creditDebit, null)).toString();  


        if (reportCreditDebit != null) {
            if (reportCreditDebit == "Credit") {
                    whichReport = "credit";
            }
            else if (reportCreditDebit == "Debit") {
                    whichReport = "debit";
            }
            fullReport(whichReport);
        }

    }

    if (evt.getSource()==main_reportFull){
            whichReport = "full";
            fullReport(whichReport);
    }

    if (evt.getSource()==main_Exit){
        frame.dispose();
    }

    if (evt.getSource()==addTransaction_confirm) {
         try {
                f = new File("Report.txt");
                f.createNewFile();
            r = new FileReader(f);
            b = new BufferedReader(r);   
            fw = new FileWriter(f, true);
            bw = new BufferedWriter(fw);
            }
         catch(Exception e){
             System.out.println("File does not exist!");
            }

         String reportLine = (dateDay.getSelectedItem() + " " + dateMonth.getSelectedItem() + " " + dateYear.getSelectedItem() + " " + accountNumberField.getSelectedItem() + " " + creditDebit.getSelectedItem() + " " + amountMoney.getText() + " " + descriptionField.getText() + "\n");

         try {
             String money = amountMoney.getText();
             double moneyInt = Double.parseDouble(money);

             try {
                 bw.write(reportLine); 
                 b.close();
                 bw.close();
             }
             catch (Exception e){
                System.out.println("No save file found!");
             }
            frame2.dispose();
            }
         catch (Exception e){
            JOptionPane.showMessageDialog(null, "You Must Enter an amount of Money!", "Error", JOptionPane.ERROR_MESSAGE);
                frame2.dispose();
         }
    }

    if (evt.getSource()==addTransaction_cancel){
        frame2.dispose();
    }
}

}

其中有两个,这两个都给了我同样的错误。我已经尝试添加一个if语句来检查它是否等于null但它不起作用,仍然得到完全相同的错误。那么我该如何修复错误呢?

BTW,当我点击joptionpane上的“取消”按钮时,我只收到错误,否则我从未收到错误

谢谢!

1 个答案:

答案 0 :(得分:1)

String reportCreditDebit = (JOptionPane.showInputDialog(null, "Select Credit or Debit", "Report by Credit/Debit",
                                    JOptionPane.PLAIN_MESSAGE, null, creditDebit, null)).toString();

这里有一个问题。 JOptionPane.showInputDialog可能会返回null值。很明显,如果单击“取消”,则输入为空。所以你应该检查null。

Object temp = JOptionPane.showInputDialog(null, "Select Credit or Debit", "Report by Credit/Debit",
                                        JOptionPane.PLAIN_MESSAGE, null, creditDebit, null);    
String reportCreditDebit = temp == null ? null : temp.toString();