1

This class compiles just fine:

public class Student{
//define variables
static String name;

//define constructor
public Student(String n){
    name = n;
}

//define method to display name
public static void displayStudent(){
    System.out.println("Name: " + name);
}
}

Here is the class where I am trying to call the displayStudent() method, but I am getting java.lang.NullPointerException exception coming from the for loop :

public class MyClass{

//define array of object Student
private Student[] students;

//constructor
MyClass(int size){
Student[] students = new Student[size];
}

    //define method to display the students variable
public void displayAllStudents(){
    for (int i = 0; i <= students.length; i++){
        students[i].displayStudent();
    }
}

I am trying to fixing it with Eclipse, but it says that in MyClass "the value of the local variable students is not used". Where is my mistake?

JohnSnow
  • 163
  • 4
  • 16
  • 1
    You're creating an array, but are not filling it with objects yet, it only has null references, as you're finding out. Fill each array element first with `new Student(...)` before trying to use it. – Hovercraft Full Of Eels Oct 16 '16 at 14:17

4 Answers4

2

You are creating a local variable here

MyClass(int size){
    // only exists inside this scope.
    Student[] students = new Student[size];
}

But this is local to this constructor so you immediately throw it away. I assume you meant to set the field students

MyClass(int size) {
    students = new Student[size];
}

Note: all this does is create an array of references to Students which are all null. So what you also need is to create a Student object for each of these indexes

MyClass(int size) {
    students = new Student[size];
    for (int i = 0; i < size; i++)
        students[i] = new Student();
}

Note how the common pattern is to use < not <= Your printing loop needs to be either

for (int i = 0; i < students.length; i++){

or

for (int i = 0; i <= students.length-1; i++){

As indexes start at 0, if you have n elements, the last one will be n-1

p.s. You probably didn't mean for the variable "name" to be static. By making it static, all instances of "Student" will share the same name.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @Radik That should really be a comment on the question. – Peter Lawrey Oct 16 '16 at 14:38
  • 1
    @sirio If this answer solved your problem then please mark it as the correct answer. This is because all posts on Stack Overflow are meant to be helpful to future visitors also and posts with accepted answers are bound to be more helpful. – progyammer Oct 16 '16 at 14:41
1

It is because of that array size change that code into

public void displayAllStudents(){ for (int i = 0; i < students.length; i++){ students[i].displayStudent(); } }

if an array have 3 elements array.length function return 3. And you count it from 0 ,1,2,3 yaa there is 4 number . it will return array outof bound exception or nullpointer exception .

noobEinstien
  • 3,147
  • 4
  • 24
  • 42
  • yep as soon as I fixed it i got Arrayoutofbounds, so i fixed it with `students.length -1` and now works. thanks! – JohnSnow Oct 16 '16 at 14:23
1

See the link for solution: https://github.com/omkar-nibandhe/StackOverflowSolutions/commit/c7aa250caeeed6a3827e0579b174aae59d4f9c22

Changes are made in your class : "MyClass" for the declaration part of Student[].

-You are calling the constructor with size of array, so initialize the Student[] with size of your choice (hard-coded to 10 in this case).

Hope this helps you.

0

As we can see you are using the array of students but you are not calling the constructor of student class(with argument) and hence they contain null as stored value in name field and gives you a null pointer exception.

   class MyClass{

//define array of object Student
private Student[] students;

//constructor
MyClass(int size){
Student[] students = new Student[size];
}

    //define method to display the students variable
public void displayAllStudents(){
    for (int i = 0; i <= students.length; i++){
        students[i].displayStudent();//this will give null pointer execption because student 
        // objects are not inisialised and cointains null 
    }
}
}

but this code will not give null pointer exception.

    class MyClass{

//define array of object Student
private Student[] students;

//constructor
MyClass(int size){
Student[] students = new Student[size];
for(int i=0;i<size;i++){
     students[i]=new Student("the string value");
}
}

    //define method to display the students variable
public void displayAllStudents(){
    for (int i = 0; i <= students.length; i++){
        students[i].displayStudent();
    }
}
}