0

I'm getting the NullPointerException when I use this method. Someone told me it is because student.getId() returns a null. I have tried to fix this, but I can't figure it out. Below is just a snippet of the code, just the method and the Student class.

edit: I added the part where the array was created.

Student[] students ;

public Student[] enterStudents(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of students");
    int numOfStudents = input.nextInt();
    Student[] students = new Student[numOfStudents];
    int i;
    for(i = 0; i <= numOfStudents - 1; i++){
        System.out.println("Enter student's ID: ");
        int id = input.nextInt();
        System.out.println("Enter student's first name: ");
        String first = input.next();
        System.out.println("Enter student's last name: ");
        String last = input.next();
        System.out.println("Enter student's class: ");
        String stuClass = input.next();
        Student x = new Student(id,first,last,stuClass);
        students[i] = x;
    }

    return students;

}




public void retrieveStuId(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter student id");
    int searchID = input.nextInt();
    int i;
    for(i = 0; i < students.length;  i++){
        Student student = students[i];
        int search = student.getId();
        if (search == searchID) {
            System.out.println(student.toString());
        }
    }
}

class Student{
    private int studentID;
    private String firstName;
    private String lastName;
    private String stuClass;

    public Student(){
    }

    public Student(int id, String first, String last, String c ){
        studentID = id;
        firstName = first;
        lastName = last;
        stuClass = c;
    }

    public void setID (int id){
        studentID = id;
    }

    public void  setStuClass (String c){
        stuClass = c;
    }

    public void setFirst(String first){
        firstName = first;
    }

    public void setLast(String last){
        lastName = last;
    }

    public String getFirst(){
        return firstName;
    }

    public String getLast(){
        return lastName;
    }

    public int getId(){
        return studentID;
    }

    public String getStuClass(){
        return stuClass;
    } 

    public String toString(){
        return "Student ID: " + studentID + " --- " + "Student Name: " + firstName + " " + lastName + " --- " + "Class:" + stuClass;
    }
}

Thank for any help in advance.

9
  • 7
    The NullPointerException should come with a stack trace. The stack trace gives you the precise line where the NullPointerException occurs. Please let us know which one it is. Commented Dec 5, 2011 at 12:18
  • Where do you create your Student objects? Which line throws the error? What do you see in your debugger on the line which throws the error? Commented Dec 5, 2011 at 12:20
  • 1
    where does students come from? Are null in students[i] ?? Commented Dec 5, 2011 at 12:21
  • @S.L.Barth it says it line 43, right where the for loop starts. Commented Dec 5, 2011 at 12:22
  • 1
    As said by the others, the stacktrace reveals at which line in your code the NPE occurs. And I would not listen to the person who told you that the Student#getId method returns null, since that method returns an int, which can not be null Commented Dec 5, 2011 at 12:23

5 Answers 5

5

Your students array has null values, which you try to dereference. The bug isn't in the code you posted, rather where the students array is created and filled.

Just check for null values, and print something like "student not found."

for(i = 0; i < students.length;  i++){         
   Student student = students[i];             
   if ( student != null ) {
      int search = student.getId();             
      if (search == searchID)                 
          System.out.println(student.toString());      
   }
} 

EDIT: I checked your code, it works, I tested it by adding

public class StudentTest {

        public static void main(String[] args) {
            StudentTest s = new StudentTest();
        }

        public StudentTest() {
            students = enterStudents();
            retrieveStuId();

        }
        // your code here ...
        Student[] students ;
            // .... end
    }

Check the place where you assign the array returned by enterStudents.

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

1 Comment

I added the part where the array was created if you could take a look at it and tell me if you see anything wrong you help would be appreciated.
1

There are two problem with that code. First is related to shadowing as mentioned before. Second, as long as Im concerned about this code there is problem with not assigned return type to variable. Basically I think you forgotten to assigned return from your method enterStudents to your variable. Hopefully it is clear for you :)

1 Comment

Thanks for your help/input. I appreciate everyone help. I ended up reworking the program moving the array out of the enterStudents method and created a different method just for the array. I got almost everything working, just wasn't able to finish the entire project, I'm still missing one method, but hopefully I will get at least an 80% on it.
0

There is an undefined (null) Student in your students array.Check if it's not null then use getID method.

Comments

0

How did you create the student array? Is it initialized with a Student for every position?

To find out print the value of student after this line:

 Student student = students[i];

with

 System.out.println(student);

for example. Also check whether all the students in the array have been initialized with a correct ID so getIt() returns a non-null value. (try printing the value after assigning to search).

Comments

0

You have two (that is TWO) declarations for something called students. One of them (the local variable) is being initialized, and the other (the instance variable) is not being initialized. The NullPointerException is being thrown when you try to use the one that is not initialized.

You shouldn't have two declarations.

Since this is homework, I'll leave it to you to figure out which declaration to delete ... and what to do about the other one.

2 Comments

I don'y know how to return the array and keep so the other methods can use it, and this project is due in 45 minutes. Can I have another hint?
Never mind I got just don't have to fix it, thanks for the help though.

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.