NullPointerException error

Discussion in 'Java' started by luch, Sep 25, 2005.

  1. luch

    luch Guest

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

    }
     
    luch, Sep 25, 2005
    #1
    1. Advertising

  2. luch

    Michael Dunn Guest

    "luch" <> wrote in message
    news:...
    > 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"));
     
    Michael Dunn, Sep 25, 2005
    #2
    1. Advertising

  3. luch

    luch Guest

    that's just what I needed - thank you very much.
     
    luch, Sep 25, 2005
    #3
  4. luch

    Roedy Green Guest

    Roedy Green, Sep 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon A. Cruz

    Re: NullPointerException Error??

    Jon A. Cruz, Jul 6, 2003, in forum: Java
    Replies:
    0
    Views:
    568
    Jon A. Cruz
    Jul 6, 2003
  2. Tohru Kao
    Replies:
    3
    Views:
    472
    Neil Masson
    Jul 14, 2003
  3. Tohru Kao
    Replies:
    1
    Views:
    428
    Chris
    Jul 8, 2003
  4. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,282
    Dhek Bhun Kho
    Jul 9, 2003
  5. Tom
    Replies:
    12
    Views:
    8,259
    Chris Smith
    Aug 5, 2003
Loading...

Share This Page