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

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

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

问题

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

  1. public class Spread extends JPanel {
  2. private JPanel outputArea;
  3. private JTextArea numberDead;
  4. private JTextArea totalInfected;
  5. private JTextArea newInfections;
  6. private Integer totalDead;
  7. private Integer total;
  8. private Integer newInfected;
  9. private Integer length = 60;
  10. private Integer width = 100;
  11. private Cell[][] label;
  12. public void makeOutputArea() {
  13. int a;
  14. int b;
  15. outputArea = new JPanel(new GridLayout(length, width, -1, -1));
  16. outputArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
  17. for (a = 0; a < (length - 1); a++){
  18. for (b = 0; b < (width - 1); b++) {
  19. label[a][b] = new Cell(); // 空指针错误在这里。
  20. label[a][b].setPreferredSize(new Dimension(10, 10));
  21. label[a][b].setOpaque(true);
  22. outputArea.add(label[a][b]);
  23. outputArea.setVisible(true);
  24. if (label[a][b].getInfected() == true) ++total;
  25. }
  26. }
  27. }
  28. public JPanel getOutputArea() {
  29. return outputArea;
  30. }
  31. ...其他代码继续
  32. }

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

  1. import java.awt.Color;
  2. import java.util.Random;
  3. import javax.swing.JLabel;
  4. @SuppressWarnings("serial")
  5. public class Cell extends JLabel{
  6. private int iteration;
  7. private boolean infected;
  8. private boolean dead;
  9. private boolean immune;
  10. private Random generator = new Random(); // 随机数生成器
  11. private static int infectionRate = 5;
  12. private static int deathRate = 3;
  13. public void setIteration(int i) {
  14. iteration = i;
  15. }
  16. public int getIteration() {
  17. return iteration;
  18. }
  19. public void setInfected(boolean b) {
  20. infected = b;
  21. }
  22. public boolean getInfected() {
  23. return infected;
  24. }
  25. public void setDead(boolean a) {
  26. dead = a;
  27. }
  28. public boolean getDead() {
  29. return dead;
  30. }
  31. public void setImmune(boolean i) {
  32. immune = i;
  33. }
  34. public boolean getImmune() {
  35. return immune;
  36. }
  37. public Cell() {
  38. setImmune(false);
  39. setDead(false);
  40. setIteration(0);
  41. if (generator.nextInt(100) < infectionRate) {
  42. setInfected(true);
  43. setIteration(++iteration);
  44. }
  45. else setInfected(false);
  46. color();
  47. }
  48. public void color() {
  49. if (getInfected() == true) setBackground(Color.RED);
  50. else if (getDead() == true) setBackground(Color.BLACK);
  51. else setBackground(Color.GREEN);
  52. }
  53. public void updateInfection(boolean b) {
  54. try {
  55. if (getImmune() == true) return;
  56. else if (getDead() == true) return;
  57. else if (getInfected() == true) {
  58. setIteration(++iteration);
  59. if (iteration >= 4) {
  60. if (generator.nextInt(100) < deathRate) {
  61. setInfected(false);
  62. setDead(true);
  63. return;
  64. }
  65. else {
  66. setInfected(false);
  67. setImmune(true);
  68. return;
  69. }
  70. }
  71. return;
  72. }
  73. else {
  74. if (generator.nextInt(100) <= 75) {
  75. setInfected(true);
  76. setIteration(++iteration);
  77. return;
  78. }
  79. else return;
  80. }
  81. }
  82. finally {
  83. this.color();
  84. }
  85. }
  86. }
英文:

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.

  1. public class Spread extends JPanel {
  2. private JPanel outputArea;
  3. private JTextArea numberDead;
  4. private JTextArea totalInfected;
  5. private JTextArea newInfections;
  6. private Integer totalDead;
  7. private Integer total;
  8. private Integer newInfected;
  9. private Integer length = 60;
  10. private Integer width = 100;
  11. private Cell[][] label;
  12. public void makeOutputArea() {
  13. int a;
  14. int b;
  15. outputArea = new JPanel(new GridLayout(length, width, -1, -1));
  16. outputArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
  17. for (a = 0; a &lt; (length - 1); a++){
  18. for (b = 0; b &lt; (width - 1); b++)
  19. label[a][b] = new Cell(); //nullpointerror is located here.
  20. label[a][b].setPreferredSize(new Dimension(10, 10));
  21. label[a][b].setOpaque(true);
  22. outputArea.add(label[a][b]);
  23. outputArea.setVisible(true);
  24. if (label[a][b].getInfected() == true) ++total;
  25. }
  26. }
  27. public JPanel getOutputArea() {
  28. return outputArea;
  29. }
  30. ...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.

  1. import java.awt.Color;
  2. import java.util.Random;
  3. import javax.swing.JLabel;
  4. @SuppressWarnings(&quot;serial&quot;)
  5. public class Cell extends JLabel{
  6. private int iteration;
  7. private boolean infected;
  8. private boolean dead;
  9. private boolean immune;
  10. private Random generator = new Random(); //Random number Generator
  11. private static int infectionRate = 5;
  12. private static int deathRate = 3;
  13. public void setIteration(int i) {
  14. iteration = i;
  15. }
  16. public int getIteration() {
  17. return iteration;
  18. }
  19. public void setInfected(boolean b) {
  20. infected = b;
  21. }
  22. public boolean getInfected() {
  23. return infected;
  24. }
  25. public void setDead(boolean a) {
  26. dead = a;
  27. }
  28. public boolean getDead() {
  29. return dead;
  30. }
  31. public void setImmune(boolean i) {
  32. immune = i;
  33. }
  34. public boolean getImmune() {
  35. return immune;
  36. }
  37. public Cell() {
  38. setImmune(false);
  39. setDead(false);
  40. setIteration(0);
  41. if (generator.nextInt(100) &lt; infectionRate) {
  42. setInfected(true);
  43. setIteration(++iteration);
  44. }
  45. else setInfected(false);
  46. color();
  47. }
  48. public void color() {
  49. if (getInfected() == true) setBackground(Color.RED);
  50. else if (getDead() == true) setBackground(Color.BLACK);
  51. else setBackground(Color.GREEN);
  52. }
  53. public void updateInfection(boolean b) {
  54. try {
  55. if (getImmune() == true) return;
  56. else if (getDead() == true) return;
  57. else if (getInfected() == true) {
  58. setIteration(++iteration);
  59. if (iteration &gt;=4) {
  60. if (generator.nextInt(100) &lt; deathRate) {
  61. setInfected(false);
  62. setDead(true);
  63. return;
  64. }
  65. else {
  66. setInfected(false);
  67. setImmune(true);
  68. return;
  69. }
  70. }
  71. return;
  72. }
  73. else {
  74. if (generator.nextInt(100) &lt;= 75) {
  75. setInfected(true);
  76. setIteration(++iteration);
  77. return;
  78. }
  79. else return;
  80. }
  81. }
  82. finally {
  83. this.color();
  84. }
  85. }
  86. }

答案1

得分: 0

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

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

  1. 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:

  1. 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:

确定