- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每当我在我正在处理的一段代码上按下按钮时,我都会收到以下错误。仅当我尝试使用 random.nextInt(20) 部分时它才会出现,因为删除它会使它不会发生。
我想知道为什么会出现此错误,以及如何解决它。错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SkelePackage.SkeletonShooter$SaveListener.actionPerformed(SkeletonShooter.java:336)
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)
代码本身是:
package SkelePackage;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.security.SecureRandom;
import java.util.Random;
import javax.swing.JComponent;
import java.awt.event.KeyEvent;
import javax.swing.JTabbedPane;
public class SkeletonShooter extends JFrame {
private static final int FRAME_WIDTH = 1000; //setting frame width and height
private static final int FRAME_HEIGHT = 550;
private static final int AREA_ROWS = 20; //setting text area width and height
private static final int AREA_COLUMNS = 40;
private int hitCounter = 0;
private int critCounter = 0;
private int totalDamage = 0;
private int saveCounter = 0;
private JButton shoot;
private JButton clear;
private JButton saveButton;
private JCheckBox advantage;
private JCheckBox disadvantage;
private JCheckBox resistance;
private JLabel skeleLabel;
private JLabel skeleLabel2;
private JTextField skeletons;
private JTextField skeletons2;
private JLabel acLabel;
private JTextField ac;
private JLabel dcLabel;
private JTextField dc;
private JLabel hitsLabel;
private JTextField hits;
private JLabel damLabel;
private JTextField damage;
private JTextArea hitsArea;
private JTextArea damArea;
private JLabel tHitsLabel;
private JTextField tHitsField;
private JLabel tDamLabel;
private JTextField tDamField;
private JLabel failsLabel;
private JTextField fails;
private JComboBox < String > comboBox;
private JComboBox < String > comboBox2;
private ActionListener listener;
private SecureRandom random;
private int roll;
private int roll2;
private int damRoll;
private int save = 0;
private skeleStats stats = new skeleStats();
private weapon wep = new weapon();
public SkeletonShooter() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Attack", createControlPanel());
tabbedPane.add("Save", createFields3());
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
add(tabbedPane);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("SkeletonShooter1.0");
}
public JPanel createAreas() {
hitsArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
hitsArea.setText("");
hitsArea.setEditable(false);
damArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
damArea.setText("");
damArea.setEditable(false);
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(hitsArea);
JScrollPane scrollPane2 = new JScrollPane(damArea);
panel.add(scrollPane);
panel.add(scrollPane2);
return panel;
}
public JPanel createFields2() {
tHitsLabel = new JLabel("Total Hits:");
final int FIELD_WIDTH = 10;
tHitsField = new JTextField(FIELD_WIDTH);
tHitsField.setText("");
tHitsField.setEditable(false);
tDamLabel = new JLabel("Total Damage:");
tDamField = new JTextField(FIELD_WIDTH);
tDamField.setText("");
tDamField.setEditable(false);
JPanel panel = new JPanel();
panel.add(tHitsLabel);
panel.add(tHitsField);
panel.add(tDamLabel);
panel.add(tDamField);
return panel;
}
public JPanel createFields() {
skeleLabel = new JLabel("Skeletons:");
final int FIELD_WIDTH = 10;
skeletons = new JTextField(FIELD_WIDTH);
skeletons.setText("" + 0);
acLabel = new JLabel("AC:");
ac = new JTextField(FIELD_WIDTH);
ac.setText("" + 0);
JPanel panel = new JPanel();
panel.add(skeleLabel);
panel.add(skeletons);
panel.add(acLabel);
panel.add(ac);
return panel;
}
public JPanel createComboBox() {
comboBox = new JComboBox < > ();
comboBox.addItem("Heavy Crossbow");
comboBox.addItem("Short Sword");
comboBox.setEditable(false);
ActionListener comboListen = new ComboListener();
comboBox.addActionListener(comboListen);
comboBox.setSelectedIndex(0);
JPanel panel = new JPanel();
panel.add(comboBox);
return panel;
}
class ComboListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int theIndex = comboBox.getSelectedIndex();
if (theIndex == 0) {
wep.setHCrossbow();
} else if (theIndex == 1) {
wep.setShortSword();
}
}
}
public JPanel createCheckBoxes() {
advantage = new JCheckBox("Advantage");
advantage.addActionListener(listener);
disadvantage = new JCheckBox("Disadvantage");
disadvantage.addActionListener(listener);
resistance = new JCheckBox("Resistance");
resistance.addActionListener(listener);
JPanel panel = new JPanel();
panel.add(advantage);
panel.add(disadvantage);
panel.add(resistance);
return panel;
}
public JPanel createButtons() {
shoot = new JButton("Fire!");
ActionListener firelisten = new AddFireListener();
shoot.addActionListener(firelisten);
clear = new JButton("Clear");
ActionListener clearlisten = new AddClearListener();
clear.addActionListener(clearlisten);
JPanel panel = new JPanel();
panel.add(shoot);
panel.add(clear);
return panel;
}
class AddFireListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
totalDamage = 0;
hitCounter = 0;
critCounter = 0;
for (int n = 0; n < Integer.parseInt(skeletons.getText()); ++n) {
roll = random.nextInt(20) + 5;
roll2 = random.nextInt(20) + 5;
if (advantage.isSelected() && disadvantage.isSelected()) {
roll = roll;
} else if (advantage.isSelected()) {
if (roll2 > roll)
roll = roll2;
} else if (disadvantage.isSelected()) {
if (roll2 < roll)
roll = roll2;
}
if (roll == 24) {
++critCounter;
hitsArea.append("Skeleton " + (n + 1) + " rolled a CRIT!\n");
} else if (roll >= Integer.parseInt(ac.getText())) {
++hitCounter;
hitsArea.append("Skeleton " + (n + 1) + " rolled a " + roll + " and HIT!\n");
} else {
hitsArea.append("Skeleton " + (n + 1) + " rolled a " + roll + " and MISSED!\n");
}
}
for (int i = 0; i < hitCounter; ++i) {
damRoll = wep.getDam();
if (resistance.isSelected())
damRoll = damRoll / 2;
damArea.append("Damage from hit " + (i + 1) + ": " + damRoll + ".\n");
totalDamage = totalDamage + damRoll;
}
for (int j = 0; j < critCounter; ++j) {
damRoll = wep.getCritDam();
if (resistance.isSelected())
damRoll = damRoll / 2;
damArea.append("Damage from CRIT " + (j + 1) + ": " + damRoll + ".\n");
totalDamage = totalDamage + damRoll;
}
hitsArea.append("Hits: " + (hitCounter + critCounter) + "\n");
damArea.append("Damage: " + totalDamage + "\n");
tHitsField.setText("" + (hitCounter + critCounter));
tDamField.setText("" + totalDamage);
}
}
class AddClearListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
hitCounter = 0;
critCounter = 0;
totalDamage = 0;
hitsArea.append("Cleared!\n");
damArea.append("Cleared!\n");
tHitsField.setText("" + 0);
tDamField.setText("" + 0);
skeletons.setText("" + 0);
ac.setText("" + 0);
}
}
public JPanel createFields3() {
skeleLabel2 = new JLabel("Skeletons:");
final int FIELD_WIDTH = 10;
skeletons2 = new JTextField(FIELD_WIDTH);
skeletons2.setText("" + 0);
skeletons2.setEditable(true);
dcLabel = new JLabel("DC:");
dc = new JTextField(FIELD_WIDTH);
dc.setText("" + 0);
dc.setEditable(true);
comboBox2 = new JComboBox < > ();
comboBox2.addItem("Strength");
comboBox2.addItem("Dexterity");
comboBox2.addItem("Constitution");
comboBox2.addItem("Intelligence");
comboBox2.addItem("Wisdom");
comboBox2.addItem("Charisma");
comboBox2.setEditable(false);
comboBox2.setSelectedIndex(0);
ActionListener comboListen = new ComboListener2();
comboBox2.addActionListener(comboListen);
comboBox2.setSelectedIndex(0);
saveButton = new JButton("Save");
ActionListener saveListen = new SaveListener();
saveButton.addActionListener(saveListen);
failsLabel = new JLabel("Fails");
fails = new JTextField(FIELD_WIDTH);
fails.setText("" + 0);
fails.setEditable(false);
JPanel panel = new JPanel();
panel.add(skeleLabel2);
panel.add(skeletons2);
panel.add(dcLabel);
panel.add(dc);
panel.add(comboBox2);
panel.add(saveButton);
panel.add(failsLabel);
panel.add(fails);
return panel;
}
class ComboListener2 implements ActionListener {
public void actionPerformed(ActionEvent event) {
int theIndex = comboBox2.getSelectedIndex();
if (theIndex == 0)
save = stats.strMod();
else if (theIndex == 1)
save = stats.dexMod();
else if (theIndex == 2)
save = stats.conMod();
else if (theIndex == 3)
save = stats.intMod();
else if (theIndex == 4)
save = stats.wisMod();
else if (theIndex == 5)
save = stats.chaMod();
}
}
class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
saveCounter = 0;
int roll;
for (int q = 0; q < Integer.parseInt(skeletons2.getText()); ++q) {
roll = random.nextInt(20) + 1 + save;
if (roll < Integer.parseInt(dc.getText())) {
++saveCounter;
}
}
fails.setText("" + saveCounter);
}
}
public JPanel createControlPanel() {
JPanel fields = createFields();
JPanel cboxes = createCheckBoxes();
JPanel areas = createAreas();
JPanel fields2 = createFields2();
JPanel buttons = createButtons();
JPanel combo = createComboBox();
JPanel controlPanel = new JPanel();
controlPanel.add(combo);
controlPanel.add(fields);
controlPanel.add(cboxes);
controlPanel.add(areas);
controlPanel.add(fields2);
controlPanel.add(buttons);
return controlPanel;
}
public static void main(String[] args) {
SkeletonShooter test = new SkeletonShooter();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
但我相信问题出在这个方面:
public JPanel createFields3(){
skeleLabel2 = new JLabel("Skeletons:");
final int FIELD_WIDTH = 10;
skeletons2 = new JTextField(FIELD_WIDTH);
skeletons2.setText("" + 0);
skeletons2.setEditable(true);
dcLabel = new JLabel("DC:");
dc = new JTextField(FIELD_WIDTH);
dc.setText("" + 0);
dc.setEditable(true);
comboBox2 = new JComboBox<>();
comboBox2.addItem("Strength");
comboBox2.addItem("Dexterity");
comboBox2.addItem("Constitution");
comboBox2.addItem("Intelligence");
comboBox2.addItem("Wisdom");
comboBox2.addItem("Charisma");
comboBox2.setEditable(false);
comboBox2.setSelectedIndex(0);
ActionListener comboListen = new ComboListener2();
comboBox2.addActionListener(comboListen);
comboBox2.setSelectedIndex(0);
saveButton = new JButton("Save");
ActionListener saveListen = new SaveListener();
saveButton.addActionListener(saveListen);
failsLabel = new JLabel("Fails");
fails = new JTextField(FIELD_WIDTH);
fails.setText("" + 0);
fails.setEditable(false);
JPanel panel = new JPanel();
panel.add(skeleLabel2);
panel.add(skeletons2);
panel.add(dcLabel);
panel.add(dc);
panel.add(comboBox2);
panel.add(saveButton);
panel.add(failsLabel);
panel.add(fails);
return panel;
}
class ComboListener2 implements ActionListener{
public void actionPerformed(ActionEvent event){
int theIndex = comboBox2.getSelectedIndex();
if (theIndex == 0)
save = stats.strMod();
else if (theIndex == 1)
save = stats.dexMod();
else if (theIndex == 2)
save = stats.conMod();
else if (theIndex == 3)
save = stats.intMod();
else if (theIndex == 4)
save = stats.wisMod();
else if (theIndex == 5)
save = stats.chaMod();
}
}
class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent event){
saveCounter = 0;
int roll;
for (int q = 0 ; q < Integer.parseInt(skeletons2.getText()) ; ++q){
roll = random.nextInt(20) + 1 + save;
if (roll < Integer.parseInt(dc.getText())){
++saveCounter;
}
}
fails.setText("" + saveCounter);
}
}
最佳答案
random
必须初始化:random = new SecureRandom();
关于java - 使用 SecureRandom 时线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075002/
我不确定我做错了什么。 我创建了一个计时器,用于更新 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 时,它工作得很好,但
我是一名优秀的程序员,十分优秀!