javaarraysjava.lang.class

Java: "exception in thread main java.lang.nullpointerexception"


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();
    }
}

Solution

  • 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.