0
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 !

2
  • You are not using addStudent() method anywhere, which you are intializing the array. Commented Dec 22, 2015 at 15:12
  • I would suggest you to use a ArrayList of students. It is easy to handle, that solves the problem also. Commented Dec 22, 2015 at 15:15

3 Answers 3

1

The reason why you get NullPointerException is because of private Student[] students=new Student[10];. It means that you have an Student array which has a fixed size of 10. Default Object values in Java is null. Without adding anything to the array it means you have 10 null objects in students.

If an offset in the students array is not filled yet, you will hit a null value and get an exception, because you try to invoke a method on null.

You can validate it in the loop:

        for(int i=0;i<students.length;i++){
            if(students[i] instanceof User) {
                System.out.println(students[i].getRollNumber());
                System.out.println(students[i].getName());
            }
        }

EDIT: This 'issue' can be avoided by using List<User> instead of User[]. But I can't decide for you if it makes more sense.

Sign up to request clarification or add additional context in comments.

Comments

1

I prefer the new For loop (since Java 5).

for(Student student : this.students) {

}

The new for loop works for arrays and all iterables objects, like ArrayList. You'll get only non-null objects.

For answer your question, the best practice is:

Overriding toString() in your Student Object.

@Override
public void String toString(){
    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(this.rollNumber);
    stringBuilder.append(this.name);

    return stringBuilder.toString();
}

And at your loop, just do

System.out.println(stundents[i]);

println handles null values as you want, and code turns clean.

Comments

0

Your array has 10 slots. If 3 are given values, then you have 7 slots with null. You will need to either change the type of data structure you are using, or check for nulls when printing. The below code would do a null check and then print which index in the array contains the null value.

public void showAllStudents()
{
    for(int i=0;i<students.length;i++)
    {
        if(students[i] != null) {
            System.out.println(students[i].getRollNumber());
            System.out.println(students[i].getName());
        }

        else
        {
            System.out.println("Array is null at index: " + i);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.