I am new to java and I for the life of me can't figure out what to do. I've made a class called Class(original, I know), and several of my methods all give me NullPointerException: null errors. All of my methods call information from my sister Student class but I don't think that's important.
Here is my entire code:
public class Class
{
private String name;
private Student[] studentList;
public Class()
{
studentList = new Student[0];
name = "";
}
public Class(String name, int stuCount)
{
this.name = name;
stuCount = 0;
}
public void addStudent(int stuNum, Student s)
{
studentList = new Student[stuNum];
}
public String getClassName()
{
return name;
}
public double getStudentAverage(int stuNum)
{
for (int i = 0; i < studentList.length; i++)
{
if (studentList[i].equals(stuNum))
{
return studentList[i].getAverage();
}
}
return 0.0;
}
public double getStudentAverage(String stuName)
{
for (int i = 0; i < studentList.length; i++)
{
if (studentList[i].getName().equals(stuName))
{
return studentList[i].getAverage();
}
}
return 0.0;
}
public String getStudentName(int stuNum)
{
String NAME = "" + stuNum;
for (int i = 0; i < studentList.length; i++)
{
if (NAME.equals(studentList[i]))
{
return studentList[i].getName();
}
}
return "";
}
public double getClassAverage()
{
double avgCL = 0.0;
double sum = 0.0;
for (int i = 0; i < studentList.length; i++)
{
sum += studentList[i].getAverage();
}
avgCL = sum / studentList.length;
return avgCL;
}
public String getStudentWithHighestAverage()
{
Student best = studentList[0];
for ( Student a : studentList)
{
if (a.getAverage() > best.getAverage())
{
best = a;
}
}
return best.getName();
}
public String getStudentWithLowestAverage()
{
Student worst = studentList[0];
for ( Student f : studentList)
{
if (f.getAverage() < worst.getAverage())
{
worst = f;
}
}
return worst.getName();
}
public String getFailureList(double failingGrade)
{
for (int i = 0; i < studentList.length; i++)
{
if (failingGrade > studentList[i].getAverage())
{
return studentList[i].getName();
}
}
return "";
}
public String toString()
{
return "";
}
Both my getStudentAverage() methods and my getFailureList() method all gave me that error. I've tried evrything I could to fix the problem for hours now but to no avail. Could someone please help me with this? I wouldn't be surprised if my other methods return the same problem. If it helps here is my ClassRunner so you know what its trying to ask for:
import static java.lang.System.*;
public class ClassRunner
{
public static void main( String args[] )
{
Class test = new Class("Comp Sci 1",4);
double[] stu1 = {100,90,80,60};
test.addStudent(0,new Student("Jimmy",stu1));
double[] stu2 = {100,100,80,70};
test.addStudent(1,new Student("Sandy",stu2));
double[] stu3 = {50,50,70,68,23};
test.addStudent(2,new Student("Fred",stu3));
double[] stu4 = {100};
test.addStudent(3,new Student("Sam",stu4));
out.println(test);
out.println(String.format(test.getStudentName(0) + "\'s average =
%.2f",test.getStudentAverage(0)));
out.println(String.format(test.getStudentName(1) + "\'s average =
%.2f",test.getStudentAverage(1)));
out.println(String.format(test.getStudentName(2) + "\'s average
%.2f",test.getStudentAverage(2)));
out.println(String.format(test.getStudentName(3) + "\'s average
%.2f",test.getStudentAverage(3)));
out.println(String.format("Sandy" + "\'s average %.2f",test.getStudentAverage("Sandy")));
out.println("Failure List = " + test.getFailureList(70));
out.println("Highest Average = " + test.getStudentWithHighestAverage());
out.println("Lowest Average = " + test.getStudentWithLowestAverage());
out.println(String.format("Class Average = %.2f",test.getClassAverage()));
}
}