0

I have a program that is supposed to allow the user to enter a file name, which, if correct, computes the average of the numbers within the file and prints it out. Likewise, if the user enters the wrong file name a certain number of times, then the program quits. In the program, the "try" block is supposed to instantiate the Scanner and set fileOk to true, while the "catch" block is supposed to prompt the user to reenter the file name, read the file, and increment fileTry. The first "if" statement is also supposed to make it so the average is calculated or an error message is written on the output file (outNumbers.dat). Here is the code I have so far:

package average;
import java.io.*; 
import java.util.Scanner; 
public class Try
{
  static  Scanner inFile;
  public static void main(String[] args) throws IOException
  {
    int fileTry = 0;
    String fileName;
    Scanner inName = new Scanner(System.in);
    System.out.println("Enter file name>");
    fileName = inName.nextLine();
    boolean fileOk;
    do
    {
      fileOk =  false;
      try
        {
          Scanner file = new Scanner(new File(fileName));
          fileOk = true;
        }
        catch(FileNotFoundException error)
        {
          System.out.println("Reenter file name>");
          fileName = inFile.nextLine();
          fileTry++;
        }
    } while (!fileOk && fileTry < 4);
    PrintWriter outFile = new PrintWriter(new FileWriter("outNumbers.dat"));

    if (fileName != null )
    {   
        int numDays = 0;
        double average;
        double inches = 0.0;
        double total = 0.0;
        while (inFile.hasNextFloat())
      {
        inches = inFile.nextFloat();
        total = total + inches;
          outFile.println(inches);
          numDays++;
      }

      if (numDays == 0) 
        System.out.println("Average cannot be computed " +
                         " for 0 days."); 
      else
      {
        average = total / numDays;
        outFile.println("The average rainfall over " +  
          numDays + " days is " + average); 
      }
      inFile.close();
    }
    else
      System.out.println("Error");
    outFile.close();
  }
}

And here is the contents of the correct file(inNumbers.dat):

2.3 
3.1 
0.3 
1.1 
2.2 
2.1 
0.0 
0.4 
0.76 
0.5 
1.0 
0.5

This is the output I get after running the program and entering the correct file name:

Enter file name>
inNumbers.dat
Exception in thread "main" java.lang.NullPointerException
    at average.Try.main(Try.java:40)

I am a bit lost on how to fix this :/

  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Harshal Parekh Apr 06 '20 at 21:00
  • Hi! When you write a question you should try to reduce the complexity of the problem and go to the point and also to make it as general as possible so that it will help others. Putting everything you ve got could mean you are just lazy to try to figure this out and lay the burden on others. In this case, you have a NullPointerException in line 40. So you could try to understand what's wrong with that line and if you still need help ask a more focused question. – Martin Massera Apr 06 '20 at 22:07

1 Answers1

0

NullPointerException is very common in java. It often means that you are trying to work with an object that was never initialized. In this case, infile is only declared and never initialized.

Scanner file = new Scanner(new File(fileName));

The file variable only exists within the try block, since that's where it was declared. You probably wanted to initialize infile instead.

infile = new Scanner(new File(fileName));

Dejke
  • 168
  • 1
  • 9