第一次尝试使用多个类时出现NullPointerException错误

时间:2015-07-14 20:33:59

标签: java class oop object this

我想让test <- expression( x <- 10 ) nenv <- new.env() ls(envir=nenv) #character(0) rapply(list(test), eval, envir = nenv) #[1] 10 ls(envir=nenv) #[1] "x" nenv$x #[1] 10 课程中的addStudent()方法在这里工作。

它应该将一个给定的学生添加到这个名册中。如果学生已经在名单上,或Roster,则不会更改名单并返回错误。如果成功则返回true。

名册等级:

numStudents == stopPoint

测试类:

public class Roster {
    Student[] students;
    int numStudents;
    int stopPoint;
    Course course;


    //constructor for this class initialize this roster to empty
    public Roster(int stopPoint, Course course)
    {
       this.stopPoint = stopPoint;
       this.course = course; 
    }



    //returns a string that represents the object for printing
    public String toString()
    {
       String res = "";
       for(int j = 0; j < numStudents; j++)
       {
          res = res + "\n" + students[j].toString();
       }
       return course + " " + numStudents + "/" + stopPoint+res;
    }




    //returns true if and only if the number of students in it is at stopPoint
      public boolean isFull(int numStudents, int stopPoint)
      {
         if (numStudents == stopPoint)
         {
            return true;
         }
         else
            return false;   
      }



 /*add given student to this roster if student already on roster 
    or numStudents already == stopPoint, will not change roster and return 
    false but return true if successful, else false
 */
    public boolean addStudent(Student student)
    {
        if(this.numStudents < this.stopPoint)
        {
            this.students[numStudents] = student; // here is where I get the error
            this.numStudents++;
            return true;
        }
        else
           return false;

   }
}

学生班:

public class TestRoster 
{

    public static void main(String[] args) 
    {
        Student s1 = new Student("John","Doe");     
        Course c1 = new Course(198, 111);            
        Roster r1 = new Roster(4, c1);              
        System.out.println(r1);
        testAdd(r1, s1);            
    }

    private static void testAdd(Roster r, Student s)
    {
        System.out.println(s.familyName+" "+r.addStudent(s));        
        System.out.println(r);                                      
    }   
}

最后,课程类:

public class Student 
{
    String personalName;
    String familyName;

    public Student(String pName, String fName)
    {
        personalName = pName;
        familyName = fName;
    }

    public String toString( )
    {
        return "Student: " + familyName + ", "+ personalName;
    }
}

这是错误:

public class Course 
{
    int deptNum;
    int courseNum;

    public Course(int deptNum, int courseNum)
    {
        this.deptNum = deptNum;
        this.courseNum = courseNum;
    }

    public String toString( )
    {
        return deptNum + ":" + courseNum;
    }


}

1 个答案:

答案 0 :(得分:1)

其他答案建议使用任意数字进行数组实例化,这不是一个好主意,因为你永远不知道它是否足够。

您的Roster班级知道应该有多少学生(通过构造函数),您应该使用Student初始化stopPoint数组:

Student[] students;
int numStudents;
int stopPoint;
Course course;

public Roster(int stopPoint, Course course)
{
    this.stopPoint = stopPoint;
    this.course = course; 
    this.students = new Student[this.stopPoint]
}

由于您无法触及类变量,因此可以并且应该在构造函数中初始化数组。