运行 本程序处理时出现空指针异常

nullpointerexception in processing when running this program

import java.util.Arrays.*;
int size=100;
//Student[] stud;
//SeatingChart[][] seats;
ArrayList<Student> roster;
void setup()
{
  size(500, 500);

  roster = new ArrayList<Student>();
  roster.add(new Student("Jin", 3));
  roster.add(new Student("Alta", 11));
  roster.add(new Student("Paul", 9));
  roster.add(new Student("Piar", 1));
  roster.add(new Student("Terra", 1));
  roster.add(new Student("Ayako", 9));
  roster.add(new Student("Glen", 2));
  roster.add(new Student("Fran", 1));
  roster.add(new Student("David", 4));
  roster.add(new Student("Danny", 3));
  SeatingChart apcs = new SeatingChart(roster, 5, 5);
  apcs.removeAbsentStudents(6, roster);
  apcs.drawChart(roster);
}




public class Student 
{ 
  private String name;
  private int absenceCount;

  public Student ( String nm )
  {  
    name = nm;
  }

  public Student ( String nm, int count )
  {  
    name = nm; 
    absenceCount = count;
  }

  public void setAbsenceCount( int ac )
  {   
    absenceCount = ac;
  }

  public String getName()  
  {  
    return name;
  }

  public int getAbsenceCount() 
  {  
    return absenceCount;
  } 

  public String toString()
  {  
    return name + " " + absenceCount + " ";
  }
}

public class SeatingChart
{
  private Student[][] seats; 
  public SeatingChart(ArrayList<Student> studentList, int rows, int cols)
  {
    seats = new Student[rows][cols];

    int row = 0, col = 0;
    for (int i = 0; i < studentList.size (); i++)
    {
      seats[row][col] = studentList.get(i);
      row++;
      if (row == rows)
      {
        row = 0;
        col++;
      }
    }
  }
  void drawChart(ArrayList<Student> newSeatingChart)
  {
    for (int i=0; i<6; i++) {
      for (int j=0; j<6; j++) {
        stroke(0);
        fill(255);
        rect(i*size, j*size, size, size);
        fill(0);
        stroke(0);
        for (int k=0; k<newSeatingChart.size (); i++) {
          text(newSeatingChart.get(i).name, i*size, j*size);
          text(newSeatingChart.get(i).absenceCount, i*size+40, j*size);
        }
      }
    }
  }


  /** Removes students who have more than a given number of absences from the
   * seating chart, replacing those entries in the seating chart with null
   * and returns the number of students removed.
   * @param allowedAbsences an integer >= 0
   * @return number of students removed from seats
   * Postcondition:
   * - All students with allowedAbsences or fewer are in their original positions in seat
   * - No student in seats has more than allowedAbsences absences.
   * - Entries without students contain null.
   */
  public int removeAbsentStudents(int allowedAbsences, ArrayList<Student> l)
  {
    int removedCount = 0; 
    for (int r = 0; r < seats.length; r++)
    {
      for (int c = 0; c < seats[0].length; c++)
      {
        l.set(r, seats[r][c]);
        if (seats[r][c] != null && seats[r][c].getAbsenceCount() > allowedAbsences)
        {
          seats[r][c] = null;
          removedCount++;
        }
      }
    }
    return removedCount;
  }

  /** Rearrange students in the seating chart in alphabetical order
   * with a new set of rows and columns
   * and returns the new seating chart. The original seating chart
   * is not affected.
   * @param rows - may be different from the original number of rows
   * @param col - may be different from the original number of columns
   * Postcondition:
   * - All students with be in the new seating chart
   * - The original seating chart is the same
   */
  public Student[][] rearrangedStudents(int rows, int cols)
  {
    Student[][] updatedList=new Student[rows][cols];
    for (int i=0; i<seats[0].length; i++) {
      for (int j=0; j<seats.length; j++) {
        if (seats[i][j].name.charAt(0)<seats[i+1][j+1].name.charAt(0)) {
          seats[i][j]=seats[i+1][j+1];
          seats[i+1][j+1]=seats[i][j];
        }
      }
    }
    return updatedList;
  }

  public String toString()
  { 
    return "";
  }
}

问题是,执行后。它将显示学生 class header 并突出显示 private String name; 行。我不确定为什么会这样,因为它是 class。我也正确使用过它。正如您在构造函数中看到的那样。那么,有人可以修复此代码或告诉我应该如何修复它吗?

Processing 编辑器并不总是擅长提供反馈。它告诉你你得到了一个 NullPointerException,它正在尝试猜测异常发生的位置。

问题是,它告诉你的是错误的。突出显示的行不是 NullPointerException 发生的地方。事实上,异常不能发生在该行。

这是因为Processing首先要将你的Processing代码编译成Java代码,并不是一对一的映射。然后它运行那个 Java 代码,这就是你的 NullPointerException 发生的地方。但它不能总是返回并告​​诉您处理代码中异常的原因是什么。换句话说:对突出显示的行持保留态度

但是,您代码中的某些内容确实导致了 NullPointerException。这就是为什么进行 增量调试 总是一个好主意。与其编写整个项目然后对其进行测试,不如编写 一小部分 并对其进行测试。我一般是每加一行代码就测试。经常测试。这样你就可以确切地知道是哪一行导致了异常。

对于你的情况,老实说,如果我是你,我会从一个干净的草图开始。然后尝试将 一小部分 复制到该草图中。 运行那个。用打印语句测试它。如果它按您期望的那样工作,则添加下一小部分。重复该过程,直到您点击 NullPointerException,然后您就会确切地知道是哪一行处理代码导致了问题。