2017-12-16 15 views
0

私のプログラムでは、正しく動作する入力ダイアログを持つJOptionPaneを持っていますが、「キャンセル」をクリックするたびにこのエラーが表示されます:スレッド「AWT-EventQueue-0」で例外が発生するjava.lang.NullPointerExceptionエラー

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(); 
    } 
} 

} 

あり、これらの二つがあり、それらの両方は私に彼と同じエラーを与える:エラーがどこにある

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) 

がここにあります。私はそれがnullと等しいかどうかを確認するためにif文を追加しようとしましたが、それは動作しませんでしたが、まったく同じエラーを持っています。どうすればエラーを修正できますか?私はJOptionPaneの上、「キャンセル」ボタンを押したときに

ところで、私はエラーを取得し、私はエラーそう

感謝を取得することはありません!

+0

私はそのスレッドを見ましたが、何をすべきかまだ分かりません。 – Arjun

+0

ここで、341行はどこですか? – msrd0

+0

文字列reportCreditDebit =(JOptionPane.showInputDialog(null、 "Select Credit or Debit"、 " JOptionPane.PLAIN_MESSAGE、null、creditDebit、null))。toString(); – Arjun

答えて

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

ここに問題があります。 JOptionPane.showInputDialognullの値を返します。キャンセルをクリックすると、入力が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(); 
関連する問題