我正在尝试使用名为molenum的变量访问对象数组中的对象。但是当我尝试运行程序时,我得到一个Null指针异常。我随机生成molenum然后使用它作为数组的索引。我出错的任何想法?
以下是初始化我的对象的代码:
MoleButton[] molebuttons = new MoleButton[25];
molebuttons[0] = new MoleButton();
molebuttons[1] = new MoleButton();
molebuttons[2] = new MoleButton();
molebuttons[3] = new MoleButton();
molebuttons[4] = new MoleButton();
molebuttons[5] = new MoleButton();
molebuttons[6] = new MoleButton();
molebuttons[7] = new MoleButton();
molebuttons[8] = new MoleButton();
molebuttons[9] = new MoleButton();
molebuttons[10] = new MoleButton();
molebuttons[11] = new MoleButton();
molebuttons[12] = new MoleButton();
molebuttons[13] = new MoleButton();
molebuttons[14] = new MoleButton();
molebuttons[15] = new MoleButton();
molebuttons[16] = new MoleButton();
molebuttons[17] = new MoleButton();
molebuttons[18] = new MoleButton();
molebuttons[19] = new MoleButton();
molebuttons[20] = new MoleButton();
molebuttons[21] = new MoleButton();
molebuttons[22] = new MoleButton();
molebuttons[23] = new MoleButton();
molebuttons[24] = new MoleButton();
buttonSpace.add(molebuttons[0]);
buttonSpace.add(molebuttons[1]);
buttonSpace.add(molebuttons[2]);
buttonSpace.add(molebuttons[3]);
buttonSpace.add(molebuttons[4]);
buttonSpace.add(molebuttons[5]);
buttonSpace.add(molebuttons[6]);
buttonSpace.add(molebuttons[7]);
buttonSpace.add(molebuttons[8]);
buttonSpace.add(molebuttons[9]);
buttonSpace.add(molebuttons[10]);
buttonSpace.add(molebuttons[11]);
buttonSpace.add(molebuttons[12]);
buttonSpace.add(molebuttons[13]);
buttonSpace.add(molebuttons[14]);
buttonSpace.add(molebuttons[15]);
buttonSpace.add(molebuttons[16]);
buttonSpace.add(molebuttons[17]);
buttonSpace.add(molebuttons[18]);
buttonSpace.add(molebuttons[19]);
buttonSpace.add(molebuttons[20]);
buttonSpace.add(molebuttons[21]);
buttonSpace.add(molebuttons[22]);
buttonSpace.add(molebuttons[23]);
buttonSpace.add(molebuttons[24]);
这是我尝试通过数组访问它们的地方。
int molecount = 0;
int molenum;
Random rand = new Random();
int i; //loop counter
do
{
MoleButton.wait(500);
while(true) {
molenum = rand.nextInt(25);
if(molebuttons[molenum].isAMole() == false)
{
molebuttons[molenum].setIsMole(true);
break;
}
}
堆栈跟踪是:
Exception in thread "main" java.lang.NullPointerException
at CW3.MoleButton.main(MoleButton.java:128)
第128行:
if(molebuttons[molenum].isAMole() == false)
该课程的完整源代码:
package CW3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MoleButton extends ColorButton {
private Boolean currentlyIsMole;
public static void main(String[] args)
{
//Create GUI
JFrame guiWindow = new JFrame();
guiWindow.setVisible(true);
guiWindow.setTitle("Jack Nicholson");
guiWindow.setLayout(new BorderLayout());
guiWindow.pack();
JPanel topBar = new JPanel();
JPanel buttonSpace = new JPanel();
guiWindow.add(topBar,BorderLayout.NORTH);
guiWindow.add(buttonSpace,BorderLayout.SOUTH);
//GUI: topbar
topBar.setLayout(new GridLayout(1,5));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
JLabel label5 = new JLabel();
topBar.add(label1);
topBar.add(label2);
topBar.add(label3);
topBar.add(label4);
topBar.add(label5);
//GUI:button space
buttonSpace.setLayout(new GridLayout(5,5));
MoleButton[] molebuttons = new MoleButton[25];
molebuttons[0] = new MoleButton();
molebuttons[1] = new MoleButton();
molebuttons[2] = new MoleButton();
molebuttons[3] = new MoleButton();
molebuttons[4] = new MoleButton();
molebuttons[5] = new MoleButton();
molebuttons[6] = new MoleButton();
molebuttons[7] = new MoleButton();
molebuttons[8] = new MoleButton();
molebuttons[9] = new MoleButton();
molebuttons[10] = new MoleButton();
molebuttons[11] = new MoleButton();
molebuttons[12] = new MoleButton();
molebuttons[13] = new MoleButton();
molebuttons[14] = new MoleButton();
molebuttons[15] = new MoleButton();
molebuttons[16] = new MoleButton();
molebuttons[17] = new MoleButton();
molebuttons[18] = new MoleButton();
molebuttons[19] = new MoleButton();
molebuttons[20] = new MoleButton();
molebuttons[21] = new MoleButton();
molebuttons[22] = new MoleButton();
molebuttons[23] = new MoleButton();
molebuttons[24] = new MoleButton();
buttonSpace.add(molebuttons[0]);
buttonSpace.add(molebuttons[1]);
buttonSpace.add(molebuttons[2]);
buttonSpace.add(molebuttons[3]);
buttonSpace.add(molebuttons[4]);
buttonSpace.add(molebuttons[5]);
buttonSpace.add(molebuttons[6]);
buttonSpace.add(molebuttons[7]);
buttonSpace.add(molebuttons[8]);
buttonSpace.add(molebuttons[9]);
buttonSpace.add(molebuttons[10]);
buttonSpace.add(molebuttons[11]);
buttonSpace.add(molebuttons[12]);
buttonSpace.add(molebuttons[13]);
buttonSpace.add(molebuttons[14]);
buttonSpace.add(molebuttons[15]);
buttonSpace.add(molebuttons[16]);
buttonSpace.add(molebuttons[17]);
buttonSpace.add(molebuttons[18]);
buttonSpace.add(molebuttons[19]);
buttonSpace.add(molebuttons[20]);
buttonSpace.add(molebuttons[21]);
buttonSpace.add(molebuttons[22]);
buttonSpace.add(molebuttons[23]);
buttonSpace.add(molebuttons[24]);
//Game loop
int molecount = 0;
int molenum;
Random rand = new Random();
int i; //loop counter
do
{
MoleButton.wait(500);
while(true) {
molenum = rand.nextInt(25);
if(molebuttons[molenum].isAMole() == false)
{
molebuttons[molenum].setIsMole(true);
break;
}
}
//Gameloop:count moles
for(i=0;i <= 24;i++)
{
if(molebuttons[i].isAMole() == true)
{
molecount++;
}
}
}while(molecount <= 8);
}
public MoleButton() {
super(100, 100, Color.RED, 10, Color.BLACK);
this.addActionListener(new ClickMole(this));
// Constructor
}
public Boolean isAMole()
{
return currentlyIsMole;
}
public synchronized void setIsMole (Boolean isMole)
{
//If true set to mole, if false set to normal button
if(isMole == true)
{
currentlyIsMole = true;
super.setColor(Color.RED);
}
else
{
currentlyIsMole = false;
super.setColor(Color.GREEN);
}
}
public static void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
public class ClickMole implements ActionListener {
//Event listener for MoleButton
MoleButton moleB;
public ClickMole(MoleButton mole)
{
moleB = mole;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (moleB.currentlyIsMole == true)
moleB.setIsMole(false);
}
}
}
答案 0 :(得分:1)
在Java中创建数组时,每个元素都初始化为null。您需要自己用实例填充数组。
现在你已经添加了源代码,问题在于布尔值,这是一个可以为空的对象,如果没有初始化则为null。 Java会自动将布尔(对象)转换为布尔(基元),如果布尔为空则失败。
因此,将布尔值更改为布尔值无处不在,除非事实上它未初始化是相关的。
答案 1 :(得分:0)
我想你已经忘记了初始按钮空间..剩下的一切看起来都不错。 你得到nullpointer是什么线?