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("-------------------");
    }
}
Rohit Sharma
  • 73
  • 1
  • 7
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jashaszun Jun 17 '15 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 – Rohit Sharma Jun 17 '15 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. – Rohit Sharma Jun 17 '15 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. – Jon Skeet Jun 17 '15 at 16:54
  • 2
    Additionally, please read the help explaining how to format code - it's *not* done with quoting (`> ...`) – Jon Skeet Jun 17 '15 at 16:56
  • @JonSkeet I tried formatting that way but there was some problem , I wasn't able to post the question. I'll learn the proper way. – Rohit Sharma Jun 17 '15 at 17:06

1 Answers1

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();
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I am not getting any error in the normal for loop. Is there any way to do this using enhanced for loop ? – Rohit Sharma Jun 17 '15 at 17:01
  • 1
    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. – rgettman Jun 17 '15 at 17:05
  • That means only values can be accessed using enhanced for loop but can't be assigned ? – Rohit Sharma Jun 17 '15 at 17:09
  • 1
    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. – rgettman Jun 17 '15 at 17:12