运行此程序时处理中的nullpointerexception(nullpointerexception in processing when running this program)

编程入门 行业动态 更新时间:2024-10-25 16:29:33
运行此程序时处理中的nullpointerexception(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 ""; } }

问题是,执行后。 它将显示学生类标题并突出显示行私有String name;。 我不确定为什么这是一个班级。 我也正确使用它。 正如你可以看到构造函数。 那么,有人可以修复此代码或告诉我应该如何修复它?

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 ""; } }

The issue is that, after executing. It will show the student class header and highlight the line private String name;. I am not sure why this is as it is a class. I have also properly used it. as you can see with the constructors. So can, someone fix this code or tell me how I should fix it?

最满意答案

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

问题是,它告诉你的是错的。 突出显示的行不是NullPointerException发生的位置。 实际上,在该行上不会发生异常。

这是因为Processing首先必须将Processing代码编译成Java代码,而不是一对一的映射。 然后它运行该Java代码,这是您发生NullPointerException地方。 但它不能总是返回并告​​诉您在处理代码中异常的原因在哪里。 换句话说: 用一粒盐突出显示它的线条

但是,代码中的某些内容确实会导致NullPointerException 。 这就是为什么进行增量调试总是一个好主意。 您应该编写一个小部件并对其进行测试,而不是编写整个项目然后进行测试。 我每次添加一行代码时都会测试。 经常测试。 这样你就可以准确地知道什么行会导致异常。

老实说,如果我是你,我会从干净的草图开始。 然后尝试将一个小部分复制到该草图中。 跑吧。 用打印语句测试它。 如果这符合您的预期,那么添加下一个小部分。 重复该过程,直到您遇到NullPointerException ,然后您将确切知道导致问题的处理代码行。

The Processing editor isn't always great at providing feedback. It's telling you that you're getting a NullPointerException, and it's trying to guess where that exception is happening.

The problem is, what it's telling you is wrong. The line that's highlighted is not where the NullPointerException is happening. In fact, an exception can't occur on that line.

This is because Processing first has to compile your Processing code into Java code, and it's not a one-to-one mapping. It then runs that Java code, which is where your NullPointerException is happening. But it can't always go back and tell you where in your Processing code the cause of the exception is. In other words: take the line it's highlighting with a grain of salt.

However, something in your code is indeed causing a NullPointerException. This is why it's always a good idea to do incremental debugging. Instead of writing your entire project and then testing it, you should write one small part and test that. I usually test every time I add a line of code. Test often. That way you'll know exactly what line causes an exception.

For your case, honestly, if I were you I'd start over with a clean sketch. Then try copying one small part over into that sketch. Run that. Test it with print statements. If that works how you'd expect it to, then add the next small part. Repeat that process until you hit the NullPointerException, and then you'll know exactly what line of Processing code is causing the problem.

更多推荐

本文发布于:2023-04-29 11:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   nullpointerexception   processing   running   program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!