gpt4 book ai didi

java - 使用 SecureRandom 时线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常

转载 作者:行者123 更新时间:2023-11-30 07:44:54 27 4
gpt4 key购买 nike

每当我在我正在处理的一段代码上按下按钮时,我都会收到以下错误。仅当我尝试使用 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/

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