我在初始化一个对象数组网格时遇到了空指针错误。

huangapple go评论70阅读模式
英文:

I am getting a null point error when initializing an array grid of objects

问题

我试图初始化一个对象数组网格,并在这样做时出现了空指针错误。应该在每次循环中调用Cell中的对象构造函数。而实际上我却得到了错误。请帮忙。

public class Spread extends JPanel {
	private JPanel outputArea;
	private JTextArea numberDead;
	private JTextArea totalInfected;
	private JTextArea newInfections;
	private Integer totalDead;
	private Integer total;
	private Integer newInfected;
	private Integer length = 60;
	private Integer width = 100;
	private Cell[][] label;
	
	public void makeOutputArea() {
		int a;
		int b;
		outputArea = new JPanel(new GridLayout(length, width, -1, -1));
		outputArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

		for (a = 0; a < (length - 1); a++){
			for (b = 0; b < (width - 1); b++) {
				label[a][b] = new Cell(); // 空指针错误在这里。
				label[a][b].setPreferredSize(new Dimension(10, 10));
				label[a][b].setOpaque(true);
				outputArea.add(label[a][b]);
				outputArea.setVisible(true);
				if (label[a][b].getInfected() == true) ++total;
			}
		}
	}
	
	public JPanel getOutputArea() {
		return outputArea;
	}
	...其他代码继续
}

这是Cell类,它应该在数组中初始化。正如您所看到的,它有一个构造函数,应该在每次循环中调用。但实际上它给出了一个空指针错误,从未初始化每个单元格对象。

import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class Cell extends JLabel{
	private int iteration;
	private boolean infected;
	private boolean dead;
	private boolean immune;
	private Random generator = new Random(); // 随机数生成器
	private static int infectionRate = 5;
	private static int deathRate = 3;
	
	
	public void setIteration(int i) {
		iteration = i;
	}
	
	public int getIteration() {
		return iteration;
	}
	
	public void setInfected(boolean b) {
		infected = b;
	}
	
	public boolean getInfected() {
		return infected;
	}
	
	public void setDead(boolean a) {
		dead = a;
	}
	
	public boolean getDead() {
		return dead;
	}
	
	public void setImmune(boolean i) {
		immune = i;
	}
	
	public boolean getImmune() {
		return immune;
	}
	
	public Cell() {
		setImmune(false);
		setDead(false);
		setIteration(0);
		if (generator.nextInt(100) < infectionRate) {
			setInfected(true);
			setIteration(++iteration);
		}
		else setInfected(false);
		color();
	}
	
	public void color() {
		if (getInfected() == true) setBackground(Color.RED);
		else if (getDead() == true) setBackground(Color.BLACK);
		else setBackground(Color.GREEN);
	}
	
	public void updateInfection(boolean b) {
		try {
			if (getImmune() == true) return;
			else if (getDead() == true) return;
			else if (getInfected() == true) {
				setIteration(++iteration);
				if (iteration >= 4) {
					if (generator.nextInt(100) < deathRate) {
						setInfected(false);
						setDead(true);
						return;
					}
					else {
						setInfected(false);
						setImmune(true);
						return;
					}
				}
				return;
			}
			else {
				if (generator.nextInt(100) <= 75) {
					setInfected(true);
					setIteration(++iteration);
					return;
				}
				else return;
			}
		}
		finally {
			this.color();
		}
	}
}
英文:

I am trying to initialize an array grid of objects and get a null point error when I do so. It should be calling the object Constructor in Cell each iteration of the loop. Instead I am getting the error. Please help.

public class Spread extends JPanel {
private JPanel outputArea;
private JTextArea numberDead;
private JTextArea totalInfected;
private JTextArea newInfections;
private Integer totalDead;
private Integer total;
private Integer newInfected;
private Integer length = 60;
private Integer width = 100;
private Cell[][] label;
public void makeOutputArea() {
int a;
int b;
outputArea = new JPanel(new GridLayout(length, width, -1, -1));
outputArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
for (a = 0; a &lt; (length - 1); a++){
for (b = 0; b &lt; (width - 1); b++)
label[a][b] = new Cell(); //nullpointerror is located here.
label[a][b].setPreferredSize(new Dimension(10, 10));
label[a][b].setOpaque(true);
outputArea.add(label[a][b]);
outputArea.setVisible(true);
if (label[a][b].getInfected() == true) ++total;
}
}
public JPanel getOutputArea() {
return outputArea;
}
...Additional code follows.

This is the Cell class it should be initializing in the array. As you can see it has a constructor, which should be called for each loop. Instead it is giving a null pointer and never initializing each cell object.

import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;
@SuppressWarnings(&quot;serial&quot;)
public class Cell extends JLabel{
private int iteration;
private boolean infected;
private boolean dead;
private boolean immune;
private Random generator = new Random(); //Random number Generator
private static int infectionRate = 5;
private static int deathRate = 3;
public void setIteration(int i) {
iteration = i;
}
public int getIteration() {
return iteration;
}
public void setInfected(boolean b) {
infected = b;
}
public boolean getInfected() {
return infected;
}
public void setDead(boolean a) {
dead = a;
}
public boolean getDead() {
return dead;
}
public void setImmune(boolean i) {
immune = i;
}
public boolean getImmune() {
return immune;
}
public Cell() {
setImmune(false);
setDead(false);
setIteration(0);
if (generator.nextInt(100) &lt; infectionRate) {
setInfected(true);
setIteration(++iteration);
}
else setInfected(false);
color();
}
public void color() {
if (getInfected() == true) setBackground(Color.RED);
else if (getDead() == true) setBackground(Color.BLACK);
else setBackground(Color.GREEN);
}
public void updateInfection(boolean b) {
try {
if (getImmune() == true) return;
else if (getDead() == true) return;
else if (getInfected() == true) {
setIteration(++iteration);
if (iteration &gt;=4) {
if (generator.nextInt(100) &lt; deathRate) {
setInfected(false);
setDead(true);
return;
}
else {
setInfected(false);
setImmune(true);
return;
}
}
return;
}
else {
if (generator.nextInt(100) &lt;= 75) {
setInfected(true);
setIteration(++iteration);
return;
}
else return;
}
}
finally {
this.color();
}
}
}

答案1

得分: 0

Cell [][] label 是 Java 中的一个二维数组。
所以你首先需要为它分配内存空间。

只有在这之后,你才能执行如下代码:

label[a][b] = new Cell();

这是关于如何正确为 Java 中的二维数组分配内存的大量信息,快速搜索会显示这个教程

英文:

Cell [][] label is a two-dimentional array in java.
So you have to allocate the memory for it first.

Only then you can execute code like:

label[a][b] = new Cell();

This is a lot of information about how to correctly allocate memory to 2-dimensional arrays in java, quick googling reveals this tutorial

huangapple
  • 本文由 发表于 2020年8月1日 06:26:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63199847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定