1

I am having three classes StudentMain, StudentService, Student. Student class contains getters and setters method and from StudentMain I'm passing Student object to StudentService. Below is the code:

code for StudentMain:

public class StudentMain {
static Student data [] = new Student[4];
static { for (int i = 0; i < data.length; i++) 
    data [i] =new Student();
    data [0] = new Student ("Sekar", new int [] {35, 35, 35}); 
    data [1] = new Student(null,new int[]{11,22,33});
    data [2] = null; 
    data [3] = new Student ("Manoj", null);

}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    StudentService studentService = new StudentService ();
    System.out.println ("Number of Objects with Marks array as null =" + studentService.findNumberOfNullMarks (data));
    System.out.println ("Number of Objects with Name as null="+ studentService.findNumberOfNullNames(data));
    System.out.println ("Number of Objects that are entirely null="+ studentService.findNumberOfNullObjects(data));
    }

}

code for Student:

public class Student {
private String name;
private int marks[];
public void setName(String name) {
    this.name=name;
}
public String getName() {
    return name;
}
public void setMarks(int [] marks) {
    this.marks=marks;
}
public int[] getMarks() {
    return marks;
}
public Student() {

}
public Student(String name,int[] marks) {
    setName(name);
    setMarks(marks);
}

}

code for StudentService:

public class StudentService{
Student[] data;
public int findNumberOfNullMarks(Student data[]) {
    this.data=data;
    int count=0;
    int i=0;
    while(i!=data.length) {
        if(data[i].getMarks()==null)
            count++;
        i++;
    }
    return count;
}
public int findNumberOfNullNames(Student data[]) {
    int count=0;
    int i=0;
    while(i!=data.length) {
        if(data[i].getName()==null)
            count++;
        i++;
    }
    return count;
}
public int findNumberOfNullObjects(Student data[]) {
    int count=0;
    int i=0;
    while(i!=data.length) {
        if(data[i]==null)
            count++;
        i++;
    }
    return count;
}

}

I am getting exception at if(data[i].getMarks()==null) and if(data[i].getMarks()=null) in StudentService class.

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27

1 Answers1

0
data [0] = new Student ("Sekar", new int [] {35, 35, 35}); 
data [1] = new Student(null,new int[]{11,22,33});
data [2] = null; 
data [3] = new Student ("Manoj", null);

for i = 0, data[0] having object and having array marks in object so you will not get java.lang.NullPointerException

for i = 1, data[1] having object and having array marks in object so you will not get java.lang.NullPointerException

for i = 2, data[2] having null so you will get java.lang.NullPointerException

for i = 3, data[3] having having object but array marks is null so you will get java.lang.NullPointerException

So you need to make sure you should have object and array.

Nitin Jangid
  • 164
  • 1
  • 3