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 "";
}
}
问题是,执行后。它将显示学生类标题并突出显示行私有String name;。我不确定为什么这是一个班级。我也正确使用它。正如你可以看到构造函数。那么,有人可以修复此代码或告诉我应该如何修复它?
答案 0 :(得分:0)
Processing编辑器并不总是很擅长提供反馈。它告诉你,你得到的是NullPointerException
,而且它正在试图猜测发生异常的位置。
问题是,它告诉你的是错的。突出显示的行不是NullPointerException
发生的位置。事实上,在该行上发生 异常。
这是因为Processing首先要将Processing代码编译成Java代码,而不是一对一的映射。然后它运行Java代码,这是您NullPointerException
正在发生的地方。但它不能总是回过头来告诉你在处理代码中,异常的原因在哪里。换句话说:用一线盐突出显示。
但是,代码中的某些内容确实会导致NullPointerException
。这就是为什么做增量调试总是一个好主意。您应该编写一小部分并对其进行测试,而不是编写整个项目然后进行测试。我通常每次添加一行代码时都会测试。经常测试。这样你就可以准确地知道哪一行会导致异常。
对于你的情况,说实话,如果我是你,我会从干净的草图开始。然后尝试将一小部分复制到该草图中。跑吧。用print语句测试它。如果这符合您的预期,那么添加下一个小部分。重复该过程,直到您点击NullPointerException
,然后您就会确切地知道处理代码的哪一行导致了问题。