I have no problems with compiling my program as it is, but I keep getting this runtime error
Exception in thread "main" java.lang.NullPointerException at Section.nPassing(Hw05.java:86) at Hw05.main(Hw05.java:28)
The output I am striving for is:
Enter name of a file of Students: Student3 In forward order: Kevin (53) Susan (82) Nguyen (75) That Section contains 3 Student(s). That Section contains 2 passing Student(s). The highest grade is 82.
My code thus far is as follows:
import java.io.*;
import java.util.Scanner;
/////////////////////////////////////////////////////////////////////////
class Hw05
{
//-----------------------------------------------------------------------
public static void main (String [] args) throws Exception
{
Scanner kb = new Scanner(System.in);
System.out.print("\nEnter name of a file of Students: ");
String filename = kb.nextLine();
Section sec = new Section(filename);
System.out.println("\nIn forward order:");
sec.print(System.out);
System.out.println("\nThat Section contains " +
sec.howMany() +
" Student(s).");
System.out.println("\nThat Section contains " +
sec.nPassing() +
" passing Student(s).");
System.out.println("\nThe highest grade is " +
sec.highestGrade() + ".");
}
//-----------------------------------------------------------------------
} // end class Hw05
/////////////////////////////////////////////////////////////////////////
class Section
{
private Student [] a;
private int used;
private static final int INIT_SIZE = 20;
//-----------------------------------------------------------------------
public Section ( String filename ) throws Exception
{
Scanner sc = new Scanner(new File(filename));
a = new Student[INIT_SIZE];
used = 0;
while ( sc.hasNext() )
{
Student s = Student.read(null,sc);
if ( used == a.length )
{
Student[] newA = new Student[2*a.length+1];
for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
newA[used] = s;
used++;
a = newA;
}
else
{
a[used] = s;
used++;
}
}
}
//-----------------------------------------------------------------------
public void print ( PrintStream ps )
{
for ( int i = 0 ; i < used ; i++ )
{
ps.println(a[i]);
}
}
//-----------------------------------------------------------------------
public int howMany() { return used; }
//-----------------------------------------------------------------------
public int nPassing()
{
int passing = 0;
for ( int i = 0; i < a.length; i++ )
{
if ( a[i].getGrade() >= 60 )
passing++;
}
return passing;
}
//-----------------------------------------------------------------------
public int highestGrade()
{
int highest = a[0].getGrade();
if ( used == 0 )
throw new Error
("Attempt to find the maximum of an empty Section");
else
{
for ( int x = 0; x < a.length; x++ )
{
if ( a[x].getGrade() > highest )
highest = a[x].getGrade();
}
}
return highest;
}
} // end class Section
/////////////////////////////////////////////////////////////////////////
class Student
{
private String name;
private int grade;
//-----------------------------------------------------------------------
public Student ( String name, int grade )
{
this.name = name;
this.grade = grade;
}
//-----------------------------------------------------------------------
public String toString ()
{
return name + " (" + grade + ")";
}
//-----------------------------------------------------------------------
public String getName() { return name; }
//-----------------------------------------------------------------------
public int getGrade() { return grade; }
//-----------------------------------------------------------------------
public void setGrade( int newGrade ) { grade = newGrade; }
//-----------------------------------------------------------------------
public static Student read ( PrintStream ps, Scanner sc )
{
if ( ps != null ) ps.println("Reading a Student record ...");
if ( ps != null ) ps.print("Enter the name: ");
String name = sc.nextLine();
if ( ps != null ) ps.print("Enter the grade: ");
int grade = sc.nextInt(); sc.nextLine();
return new Student(name,grade);
}
//-----------------------------------------------------------------------
} // end class Student
/////////////////////////////////////////////////////////////////////////

New Topic/Question
Reply



MultiQuote




|