New to JAVA. I am getting a Null Pointer Exception at line 30 in the following. I know that an array must be initialised when it is declared to avoid getting the exception. But in this case, I do not know the size of the array to be initialised. So, can someone please explain how i can rectify this error? Can you also explain about what happens when we declare and initialise a variable? import java.util.*;
class Person {
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nID: " + idNumber);
}
}
class Student extends Person{
private int[] testScores; //exception Here
Student(String firstName,String lastName,int id,int[] scores){
super(firstName,lastName,id);
for(int i=0;i<scores.length;i++)
{
testScores[i]=scores[i];
}
}
char calculate(){
int avg=0,i=0;
for(i=0;i<testScores.length;i++)
{
avg=avg+testScores[i];
}
avg=avg/(i+1);
if(avg>=90 && avg<=100)
return 'O';
else if(avg>=80 && avg<90)
return 'E';
else if(avg>=70 && avg<80)
return 'O';
else if(avg>=55 && avg<70)
return 'P';
else if(avg>=40 && avg<55)
return 'D';
else
return 'T';
}
}