Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > NullPointerException error

Reply
Thread Tools

NullPointerException error

 
 
luch
Guest
Posts: n/a
 
      09-25-2005
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_tests core.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";
}

}

 
Reply With Quote
 
 
 
 
Michael Dunn
Guest
Posts: n/a
 
      09-25-2005

"luch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hello,


>
> import java.util.*;
> import java.io.*;
>
> public class sluchini_testscore
> {
> 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 = "";


you have a class member 'inFile', and, by including the 'type' (Scanner),
you also
have a local 'inFile' (local to main)

to fix
Scanner inFile = new Scanner(new FileReader("testscores.txt"));
becomes
inFile = new Scanner(new FileReader("testscores.txt"));


 
Reply With Quote
 
 
 
 
luch
Guest
Posts: n/a
 
      09-25-2005
that's just what I needed - thank you very much.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-25-2005
On 24 Sep 2005 21:22:01 -0700, "luch" <(E-Mail Removed)> wrote or
quoted :

>NullPointerException


see http://mindprod.com/jgloss/caq.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
!s.equals("") returns nullPointerException ??? Tom Java 12 08-05-2003 02:16 PM
LinkedList NullPointerException occurs after switched from IBM JVM 1.4.0 to 1.4.1 Tohru Kao Java 3 07-14-2003 08:12 AM
Re: java.lang.NullPointerException (Tomcat) Dhek Bhun Kho Java 0 07-08-2003 11:53 PM
LinkedList NullPointerException occurs after switched from IBM JVM 1.4.0 to 1.4.1 Tohru Kao Java 1 07-08-2003 09:09 AM
Re: NullPointerException Error?? Jon A. Cruz Java 0 07-06-2003 04:27 AM



Advertisments