- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以在我的程序中,我有一个带有输入对话框的 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/
我不确定我做错了什么。 我创建了一个计时器,用于更新 GUI 顶部的文本图形。但是,当计时器用完时,它会更改一个变量(链接到一个监听器),并向我的 GUI 添加一个新的图形“场景/组/节点”。 -更改
我有一个带有 gui 的简单 JAVA 程序,它只递增 int 变量并在 JLabel 中显示其值。我通过使用 Runnable 类在其中调用 EventQueue.invokeLater() 来创建
我们遇到了一个问题,EventQueue 表有时增长得非常快,每秒最多 3k 条记录,而且从不清除记录(目前为 3000 万条)。我们的环境设置如下: 网站核心 7.2 4台CD服务器和1台CM服务器
我最近在我的机器上安装了 JDK 1.6 update 43。从那时起,每当我尝试运行事件处理程序时,我都会遇到一个问题,显示线程“AWT-EventQueue-0 中出现异常” “ 它指向哪个线程?
有人可以解释一下 EventQueue 对象在 Java Swing 中的用途吗? 我最困惑的是如何应用排队事件。也许提供事件队列的通用用途会有所帮助。 最佳答案 因为 Swing 是 single
我有一个外部设备一次向我发送 1 个字符的数据。我正在将其写入 JTextPane 上的 StyledDocument。这些数据是在不是 AWT 线程的线程上发送给我的,因此我需要创建 AWTEven
我有如下一段代码 import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import javax.swing.
我有一个扩展 JDialog 的登录表单,用户可以通过刷卡或输入用户名和密码登录。 我创建了一个 Runnable 守护程序,它与磁条阅读器通信,启动时它会请求刷卡,它会一直等到有人刷卡。如果应用程序
运行此代码时一切正常,但是当我更改内部代码以与 EventQueue.invokeLater() 一起使用时,主要问题出现了 package dav.gui; import java.awt.Bord
我有4个类(class) 用户界面/产品用户界面 Controller /产品 Controller 项目包/产品 项目包/ProductRecords 当我尝试按下“添加”按钮时,它返回一个错误,如
我对以下显示 EventQueue 行为不一致的示例代码有疑问: public static void main( String[] args ) throws InvocationTargetExc
我有一个 Java 程序占用了 100% 的 CPU,但似乎什么也没做。 如果我进行线程转储,则有 4 个线程(在 5 个线程池中)等待获取锁。 "Incoming WorkPool 5" - Thr
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
所以我试图弄清楚这个贪吃蛇游戏是如何运作的: http://zetcode.com/tutorials/javagamestutorial/snake/ 当我将代码复制到编译器中时,我在这一段代码中遇
有人可以帮我调试这个吗 编辑1:错误不在下面的代码中*以下代码中出现错误:* if (e.getSource().equals(btnRefresh)) {
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 9 年前。 Empl
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
只是尝试打印第 31 行的 LOL,或者任何人都有更好的方法从下面保存的文本文件中返回余额。请随意分享,我在这个问题上陷入了困境。出现错误: "Exception in thread "AWT-Eve
下午,我对一些 JFrame 代码有疑问,当用户按下“新用户”时,此 JFrame 就会启动,每当他们这样做时,我都会得到: Exception in thread "AWT-EventQueue-0
好的,我收到此错误 awt eventqueue 0 nullpointerexception 错误。当我尝试删除 JPanel 时。 令我困惑的是,当我删除另一个 JPanel 时,它工作得很好,但
我是一名优秀的程序员,十分优秀!