我的编码问题在这里,我无法弄清楚为什么我有它。
我正确地将ArrayList
声明为private
ArrayList,并在声明时初始化它。现在我遇到的问题是向它添加对象。每当我尝试向其添加一个对象时,我会在程序应该按照应有的方式工作时不断获得NullPointerException
。
我声明的地方
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class AdditionFrame implements ActionListener{
private JFrame frame;
private JPanel panel1, panel2,panel3;
private JMenuBar menuBar;
private JMenu mOpt1, mOpt2;
private JMenuItem mI1, mI2, mI3, mI4, mI5, mI6;
private JLabel label1,label2,label3;
private JTextField box1;
private JButton button1, button2, button3;
private int a, b, lives = 3, score = 0, max = 50, min = 35, c;
public ArrayList<HighScoreObject> hsoArray = new ArrayList<HighScoreObject>();
public AdditionFrame(JFrame f) {
// TODO Auto-generated constructor stub
this.frame = f;
initialize();
frame.setVisible(true);
}
private void initialize(){
generateRandomNumbers();
frame.setTitle("Mathematical Sums - Addition");
panel1 = new JPanel();
panel1.setLayout(new GridLayout());
frame.getContentPane().add(panel1, BorderLayout.NORTH);
label2 = new JLabel("Score: " + score);
label2.setFont(new Font("SimSun", Font.BOLD, 22));
label2.setLocation(0,0);
label2.setSize(117,37);
panel1.add(label2);
label3 = new JLabel("Lives: " + lives);
label3.setFont(new Font("SimSun", Font.BOLD, 22));
label3.setLocation(0,70);
label3.setSize(105,49);
panel1.add(label3);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
frame.getContentPane().add(panel2, BorderLayout.CENTER);
label1 = new JLabel(a +" + " +b);
label1.setFont(new Font("SimSun", Font.BOLD, 22));
label1.setHorizontalAlignment(SwingConstants.CENTER);
panel2.add(label1, BorderLayout.NORTH);
box1 = new JTextField();
box1.setLocation(249,176);
box1.setSize(96,25);
panel2.add(box1);
box1.setEditable(true);
panel3 = new JPanel();
panel3.setLayout(new GridLayout());
frame.getContentPane().add(panel3, BorderLayout.SOUTH);
button1 = new JButton("Answer");
panel3.add(button1);
button1.addActionListener(this);
button1.setLocation(187,236);
button1.setSize(105,50);
button2 = new JButton("Save Score");
panel3.add(button2);
button2.addActionListener(this);
button2.setVisible(false);
button2.setLocation(302,236);
button2.setSize(105,50);
button3 = new JButton("Reset");
panel3.add(button3);
button3.addActionListener(this);
button3.setVisible(false);
button3.setLocation(532,236);
button3.setSize(105,50);
menuBar = new JMenuBar();
mOpt1 = new JMenu("File");
menuBar.add(mOpt1);
mI1 = new JMenuItem("Return to Main");
mI1.addActionListener(this);
mOpt1.add(mI1);
mI2 = new JMenuItem("Quit");
mI2.addActionListener(this);
mOpt1.add(mI2);
mOpt2 = new JMenu("Help");
menuBar.add(mOpt2);
mI3 = new JMenuItem("Display Help");
mI3.addActionListener(this);
mOpt2.add(mI3);
mI4 = new JMenuItem("Gameplay Help");
mI4.addActionListener(this);
mOpt2.add(mI4);
frame.setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e){
String action = e.getActionCommand();
if(action.equals("Display Help")){
JOptionPane.showMessageDialog(null, "In This Mode, Random numbers are generated and added together" +
" \nAnd you have to correctly guess the right answer without losing lives " +
"\nGame is over when you run out of lives");
}
if(action.equals("Gameplay Help")){
JOptionPane.showMessageDialog(null, "In Gameplay: " +
"\n- Click on 'Answer' to check if your answer is valid" +
" \n- Green = Correct Answer " +
"\n- Red = Wrong Answer");
}
if(action.equals("Return to Main")){
frame.setVisible(false);
frame.getContentPane().removeAll();
Assignment2A g = new Assignment2A(frame);
g.getArray(hsoArray);
}
if(action.equals("Answer")){
String getAnswer = box1.getText();
int check = Integer.parseInt(getAnswer);
box1.setText("");
if(c == check){
box1.setBackground(Color.GREEN);
score++;
generateRandomNumbers();
label1.setText(String.valueOf(a) + " + " +String.valueOf(b));
label2.setText("Score: "+String.valueOf(score));
}
else if(lives <= 0){
button1.setVisible(false);
button2.setVisible(true);
button3.setVisible(true);
box1.setEditable(false);
JOptionPane.showMessageDialog(null, "Game Over, Final Score: " + String.valueOf(score));
}
else if(c != check){
box1.setBackground(Color.RED);
lives--;
label3.setText("Lives: " + String.valueOf(lives));
}
}
if(action.equals("Save Score")){
String name = JOptionPane.showInputDialog("Please Enter your name");
HighScoreObject hso = new HighScoreObject(name, "Addition", score);
System.out.println(hso.getName()+" "+hso.getType()+" "+hso.getScore());
hsoArray.add(hso);
}
if(action.equals("Reset")){
box1.setEditable(true);
box1.setBackground(Color.WHITE);
button1.setVisible(true);
button2.setVisible(false);
button3.setVisible(false);
score = 0; lives = 3;
generateRandomNumbers();
label1.setText(String.valueOf(a) + " + " +String.valueOf(b));
label2.setText("Score: "+String.valueOf(score));
label3.setText("Lives: " + String.valueOf(lives));
}
if(action.equals("Quit")){
System.exit(0);
}
}
private void generateRandomNumbers(){
Random r = new Random();
a = r.nextInt(max);
b = r.nextInt(min);
c = a + b;
System.out.println(c);
}
public void getArray(ArrayList<HighScoreObject> hsoArray) {
// TODO Auto-generated method stub
this.hsoArray = hsoArray;
}
}
这是我得到的例外
MELVCIN Addition 5
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AdditionFrame.actionPerformed(AdditionFrame.java:160)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
答案 0 :(得分:0)
创建AdditionFrame
对象时,会创建HighScoreObject
的arrayList,但在执行if(action.equals("Save Score")){
之前,会调用getArray
方法,并且它会收到null。
答案 1 :(得分:0)
看看这个存根代码:
public void getArray(ArrayList<HighScoreObject> hsoArray) {
// TODO Auto-generated method stub
this.hsoArray = hsoArray;
}
使用null参数调用getArray的可能性很大。此外,这看起来你实际上希望它的行为类似于setArray方法而不是getArray方法。
答案 2 :(得分:0)
正如DavMac在评论中建议的那样,我从主要的java代码
接收了一个数组if(action.equals("Addition")){
frame.setVisible(false);
frame.getContentPane().removeAll();
AdditionFrame p2 = new AdditionFrame(frame);
p2.getArray(array);
}
这个方法来自我的主类,并将其数组传递给我的添加类,然后当我试图将一个对象放到我的hsoArray时,程序不知道该对象放在哪里,所以它给了我异常
我已经这么久了,但是我找到了它,感谢DavMac