0

My program is getting compiled and executed without any errors and halfway through my program the error is being shown. I checked the code and it seemed to have a problem with the following part of the code:

student s[]=new student[10];
report(student s1[])
{
    this.s=s1;
}
String highmath()
{
    int temp=s[0].math;
    String name=s[0].sname;
    for(int i=0;i<s.length;i++)
    {
        if(s[i].math>temp)
        {
            temp=s[i].math;
            name=s[i].sname;
        }
    }
    return name;
}

Error exception in thread main java.lang.NullPointerException at report.highmath(stdapp.java.40)

Sachin Janani
  • 1,310
  • 1
  • 17
  • 33
Skye
  • 1

3 Answers3

1

You go to line 40 report.highmath(stdapp.java.40) and you think
what reference can be null on this line. It means you have a null
reference on that line and you're either calling a method on it, or
are trying to get one of its variables.

You can also use System.out.println to print out some references,
or use a debugger in an IDE.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Is that the only line it's spitting out? There should be more, because that line states that the error is in line 40 and your code that you gave us doesn't have 40 lines of code.

user3377890
  • 67
  • 1
  • 6
0

My guess is that s[0] or s[i] for some i is null. In other words, has s been properly initialized, before entering the highmath() method? It appears that you are trying to initialize it in the constructor:

report(student s1[])
{
    this.s=s1;
}

But are you sure that what you are sending in to the constructor was properly initialized as well?

KSK
  • 149
  • 3
  • 15