0

I am getting a null pointer exception in the selectAllStudent Method. When I am using foreach loop but When I am using a normal loop it is working fine. Please explain the reason. Thank you

Driver Class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args) {
        Student[] sdb = new Student[2];
        try {
            for (Student s : sdb) {
                s = takeInput();
            }
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        selectAllStudent(sdb);
    }

    static void selectAllStudent(Student[] sdb) {
        for (Student s : sdb) {
            s.printStudentDetails();               // Getting NullPOinterException here
        }
    }
    public static Student takeInput() throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the details ");
        System.out.print("ROLL      :"); int rollno = Integer.parseInt(in.readLine());
        System.out.print("NAME      :"); String name = in.readLine();
        System.out.print("BRANCH    :"); String branch = in.readLine();
        return (new Student(rollno,name,branch));
    }  
}

Student Class

public class Student {

    private int rollno;
    private String name;
    private String branch;

    Student(int rollno, String name, String branch) {
        this.rollno = rollno;
        this.name = name;
        this.branch = branch;
    }

    void printStudentDetails() {
        System.out.println("ROLLNO  :" + rollno);
        System.out.println("NAME    :" + name);
        System.out.println("BRANCH  :" + branch);
        System.out.println("-------------------");
    }
}
6
  • possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Jun 17, 2015 at 16:53
  • I know what is null pointer exception but I am not sure why I am getting a null pointer exception over here Commented Jun 17, 2015 at 16:54
  • If I am using a normal for loop I am not getting an exception but when I am using for each loop I am getting the exception. Commented Jun 17, 2015 at 16:54
  • 2
    @RohitSharma: If you know what a null pointer exception is, you should be able to identify which reference is null. You may then have a question about why it's null, but that wouldn't be the same question. Commented Jun 17, 2015 at 16:54
  • 2
    Additionally, please read the help explaining how to format code - it's not done with quoting (> ...) Commented Jun 17, 2015 at 16:56

1 Answer 1

3

You are not assigning a new Student to the array in this for loop.

for (Student s : sdb) {
    s = takeInput();
}

The takeInput method is returning a Student properly, but you've assigned it to a local reference s, not as an element in the array sdb. The elements of the array remain null, and the NullPointerException comes from attempting to call printStudentDetails on null in the selectAllStudent method.

You can convert the enhanced for loop to a standard for loop, assigning the Student with an array access expression.

for (int i = 0; i < sdb.length; i++)
{
    sdb[i] = takeInput();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am not getting any error in the normal for loop. Is there any way to do this using enhanced for loop ?
No, because s isn't the array element. You can change what s refers to, but that doesn't change what the array refers to.
That means only values can be accessed using enhanced for loop but can't be assigned ?
That is correct. In addition, you could call a method on Student that changes one of its fields (mutates the Student), but you still can't replace it in the array.

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.