-1

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()));
}
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Mandatory reading on NPE: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – jrook Mar 08 '20 at 06:18
  • Sorry but there are a lot of issues in your code, my suggestion to you read more about Array and how to deal with it then use it 1- your student array contain null object because of that you getting nullPointerEx 2- you assigning values to the Array in wrong way And post the exception that you're getting, it will tell you where is it come from – AMA Mar 08 '20 at 06:28
  • Also I would suggest to change your class name ```public class Class``` to some meaning ful name. – Brooklyn99 Mar 08 '20 at 06:29
  • Please mark in the code which for loop is throwing the exception. I see multiple for loops. – Trishul Singh Choudhary Mar 08 '20 at 06:29
  • @Trishul Singh Choudhary - Its more so the if statementments inside my For and For Each loops that are giving me the errors. – Keegan Dunlap Mar 08 '20 at 07:21
  • That is not your “entire code.” You forgot to show us the code for the `Student` class, which is directly relevant to your use of student numbers. – VGR Mar 08 '20 at 16:02
  • Sorry for not including my Student class, I am a little knew to this website – Keegan Dunlap Mar 08 '20 at 20:56

1 Answers1

-1

I have made changes in code and fixed the issues and it is working now

I have created this Student class as you have not provided:

class Student
{
private String name;

private double average;

public Student(String name, double[] marks)
{
    this.name = name;
    double sum = 0;
    for (double m : marks)
    {
        sum += m;
    }
    this.average = sum/marks.length;
}

public String getName() {
    return this.name;
}

public void setAge(String name) {
    this.name = name;
}

public double getAverage() {
    return this.average;
}

public void setAverage(double average) {
    this.average = average;
}

}

I have changed Class name to TestException:

public class TestException {

private String name;
private Student[] studentList;

public TestException()
{
    name = "";
    studentList = new Student[0];
}

public TestException(String name, int stuCount)
{
    this.name = name;
    studentList = new Student[stuCount];
}

public void addStudent(int stuNum, Student s)
{
    studentList[stuNum] = s;
}

public String getClassName()
{
   return name; 
}

public double getStudentAverage(int stuNum)
{
    return studentList[stuNum].getAverage();
}

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)
{
    return studentList[stuNum].getName();
}

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 "";
} }

Finally the main class is:

public class TestExceptionMain
{
public static void main( String args[] )
{
    TestException test = new TestException("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));     

    System.out.println(test);

    System.out.println(String.format(test.getStudentName(0) + "\'s average = %.2f",test.getStudentAverage(0)));    
    System.out.println(String.format(test.getStudentName(1) + "\'s average = %.2f",test.getStudentAverage(1)));    
    System.out.println(String.format(test.getStudentName(2) + "\'s average %.2f",test.getStudentAverage(2)));  
    System.out.println(String.format(test.getStudentName(3) + "\'s average %.2f",test.getStudentAverage(3)));              

    System.out.println(String.format("Sandy" + "\'s average %.2f",test.getStudentAverage("Sandy")));   

    System.out.println("Failure List = " + test.getFailureList(70));   
    System.out.println("Highest Average = " + test.getStudentWithHighestAverage());
    System.out.println("Lowest Average = " + test.getStudentWithLowestAverage());

    System.out.println(String.format("Class Average = %.2f",test.getClassAverage()));
}

}

And here is the result:

enter image description here

Muhammad bux
  • 36
  • 1
  • 6