0

I am getting an the java.lang.nullpointerexception error in my code. Though I am not sure why it is not correct. I should be a quick fix, but I can't seem to figure it out. It seems that the array in line 45 is giving me the error. Any help would be greatly appreciated. Thank you in advance.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class readFile
{
    private String fileName;
    private double lowestNum;
    private double highestNum;
    private double totalNum;
    private double averageNum;
    private int[] array;

    public readFile(String input)
    {
        fileName = input;
        lowestNum = 0;
        highestNum = 0;
        totalNum = 0;
        averageNum = 0;
    }

    public void readArray() throws FileNotFoundException
    {
        Scanner s = new Scanner(new File(fileName));
        array = new int[s.nextInt()];
        for(int i = 0; i < array.length; i++)
        {
            array[i] = s.nextInt();
        }
        s.close();
    }
    \\ERROR IS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    public void findLowest()
    {
        lowestNum = array[0];
        for(int i = 1; i < array.length; i++)
        {
            if(array[i] < lowestNum);
            lowestNum = array[i];
        }
        System.out.println("Lowest number: " + lowestNum);
    }

    public void findHighest()
    {
        highestNum = array[0];
        for(int i = 1; i < array.length; i++)
        {
            if(array[i] > highestNum);
            highestNum = array[i];
        }
        System.out.println("Highest number: " + highestNum);
    }

    public void findTotalNum()
    {
        for(int i = 0; i < array.length; i++)
        {
            totalNum = totalNum + array[i];
        }
        System.out.println("Total of all numbers: " + totalNum);
    }

    public void findAverage()
    {
        averageNum = (totalNum / array.length);
        System.out.println("Average of all numbers: " + averageNum);
    }
}

public class NumberAnalysis 
{
    public static void main(String[] args) throws FileNotFoundException 
    {
        String fileName = "Numbers.txt";

        readFile myClass = new readFile(fileName);
        myClass.findLowest();
        myClass.findHighest();
        myClass.findTotalNum();
        myClass.findAverage();
    }
}
danp32003
  • 3
  • 3

4 Answers4

3

If readArray() is the method that sets up your array, you have to call that method before you try and use the array.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

You never call readArray, so your array is never inizialized.

Jens
  • 67,715
  • 15
  • 98
  • 113
0

The problem is that you instanciate your object, but you forget to instanciate the array. You should do:

public static void main(String[] args) throws FileNotFoundException 
{
    String fileName = "Numbers.txt";

    readFile myClass = new readFile(fileName);
    myClass.readArray();
    myClass.findLowest();
    myClass.findHighest();
    myClass.findTotalNum();
    myClass.findAverage();
}

By the way, you should always have classes that starts with a capital letter, it makes your code way more readable.

nsanglar
  • 1,632
  • 1
  • 14
  • 24
0

You have to call readArray method and you should also provide it a parameter filename so that the Scanner can get the argument filename. Otherwise, it will generate a FileNotFoundException

Tarek
  • 771
  • 7
  • 18