L
luch
Hello,
I am a Java newbie, taking a class. I have to write a program that
uses methods to a) read in students test scores and average them, and
b) return an equivalvent letter grade from that average. My program
compiles fine, but when I execute it I get:
Exception in thread "main" java.lang.NullPointerException
at
sluchini_testscore.calculateAverage(sluchini_testscore.java:61)
at sluchini_testscore.main(sluchini_testscore.java:43)
I think it has something to do with the scanner reference in the main
method:
Scanner inFile = new Scanner(new FileReader("testscores.txt"));
Any insight on what I'm doing wrong would be appreciated. My code is
posted below. Please excuse any posting/etiquette errors.
Thanks
***********************************************
import java.util.*;
import java.io.*;
public class sluchini_testscore
{
static String studentName;
static String scores;
static double studentAvg;
static double classAvg;
static double studentSum;
static double classSum;
static int studentNum;
static int i;
static int nextScore;
static Scanner inFile;
public static void main (String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("testscores.txt"));
studentAvg = 0;
classAvg = 0;
studentSum = 0;
classSum = 0;
studentNum = 0;
scores = "";
PrintWriter outFile = new PrintWriter("output.txt");
outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average
Grade");
while (inFile.hasNext())
{
studentName = inFile.next();
System.out.println(studentName);
for (i = 1; i <= 5; i++)
{
calculateAverage();
studentAvg = studentSum / 5;
outFile.println(studentName + " " + scores + " " + studentAvg +
" " + calculateGrade(studentAvg));
scores = "";
classSum = classSum + studentSum;
studentNum++;
}
classAvg = classSum / studentNum;
inFile.close();
outFile.println();
outFile.println("Class Average = " + classAvg);
}
}
public static void calculateAverage()
{
nextScore=inFile.nextInt();
studentSum=studentSum + nextScore;
scores = scores + " " + nextScore;
}
public static String calculateGrade(double avg)
{
if (avg >= 90)
return "A";
else if (avg >= 80)
return "B";
else if (avg >= 70)
return "C";
else if (avg >= 60)
return "D";
else
return "F";
}
}
I am a Java newbie, taking a class. I have to write a program that
uses methods to a) read in students test scores and average them, and
b) return an equivalvent letter grade from that average. My program
compiles fine, but when I execute it I get:
Exception in thread "main" java.lang.NullPointerException
at
sluchini_testscore.calculateAverage(sluchini_testscore.java:61)
at sluchini_testscore.main(sluchini_testscore.java:43)
I think it has something to do with the scanner reference in the main
method:
Scanner inFile = new Scanner(new FileReader("testscores.txt"));
Any insight on what I'm doing wrong would be appreciated. My code is
posted below. Please excuse any posting/etiquette errors.
Thanks
***********************************************
import java.util.*;
import java.io.*;
public class sluchini_testscore
{
static String studentName;
static String scores;
static double studentAvg;
static double classAvg;
static double studentSum;
static double classSum;
static int studentNum;
static int i;
static int nextScore;
static Scanner inFile;
public static void main (String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("testscores.txt"));
studentAvg = 0;
classAvg = 0;
studentSum = 0;
classSum = 0;
studentNum = 0;
scores = "";
PrintWriter outFile = new PrintWriter("output.txt");
outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average
Grade");
while (inFile.hasNext())
{
studentName = inFile.next();
System.out.println(studentName);
for (i = 1; i <= 5; i++)
{
calculateAverage();
studentAvg = studentSum / 5;
outFile.println(studentName + " " + scores + " " + studentAvg +
" " + calculateGrade(studentAvg));
scores = "";
classSum = classSum + studentSum;
studentNum++;
}
classAvg = classSum / studentNum;
inFile.close();
outFile.println();
outFile.println("Class Average = " + classAvg);
}
}
public static void calculateAverage()
{
nextScore=inFile.nextInt();
studentSum=studentSum + nextScore;
scores = scores + " " + nextScore;
}
public static String calculateGrade(double avg)
{
if (avg >= 90)
return "A";
else if (avg >= 80)
return "B";
else if (avg >= 70)
return "C";
else if (avg >= 60)
return "D";
else
return "F";
}
}