gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:51:41 24 4
gpt4 key购买 nike

所以在我的程序中,我有一个带有输入对话框的 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,但它不起作用,仍然出现完全相同的错误。那么我该如何修复这个错误呢?

顺便说一句,只有当我点击 joptionpane 上的“取消”按钮时,我才会收到错误,否则我永远不会收到错误

谢谢!

最佳答案

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

这里有问题。 JOptionPane.showInputDialog 可能返回 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();

关于java - 在线程 "AWT-EventQueue-0"java.lang.NullPointerException 错误中获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com