public class StudentSchedular {
private Student[] students=new Student[10];
private int counterStudent;
public String addStudent(int rollNumber,String name)
{
students[counterStudent++]=new Student(rollNumber,name);
return "Student added successfully";
}
public void showAllStudents()
{
for(int i=0;i<students.length;i++){
System.out.println(students[i].getRollNumber());
System.out.println(students[i].getName());
}
}
}
I know this is a noob question..but still ! Here I have omitted the other getter/setter parts and other cases where I input the values for rollnumber and name. I am trying to print the object array, but it is giving an null pointer exception. I am inputting only 2 3 values and when I try to print, it gives NPE. I know this is because of null values being in the remaining index positions, I just needed a soln to print the whole object array !
ArrayListof students. It is easy to handle, that solves the problem also.