so in my program I have a JOptionPane with an inputdialog which works fine but whenever I click "Cancel", it gives me this error:
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)
Here's where the error is:
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();
}
}
}
There are two of these and both of them give me he same error. I've tried adding an if statement to check if it equals null but it didn't work, still got the exact same error. So how do I fix the error?
BTW, I only get the error when I hit the "Cancel" button on the joptionpane, I never get the error otherwise
Thanks!