BURNS
BURNS

Reputation: 701

Java NullpointerException in the main class

The problem:

After creating an array of FractionClass objects I try to give each Fraction a numerator and denominator ,by reading in a txtfile that looks like this:

11 12

7 24

2 3

5 6

7 9
...

The first number being the numerator and the second one being the denominator, to do this I use the Scanner class. Under the readFractions-methode i placed comments where the NULLpointerexception is located.

The mainfile:

package labo2oef5;

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


public class Labo2Oef5 {

private static Breuk [] fractionArray;
private static int amountOfLines;

    public static void main(String[] args) throws FileNotFoundException {
        lengteBestand(); // stel amountOfLines al in
        readFractions();
        System.out.println(fractionArray[4].getNumerator()+" / "+  fractionArray[4].getDenominator());`

    }

    public static void readFractions() throws FileNotFoundException{
        Scanner lezer = new Scanner(new File("breuken01.txt"));
        fractionArray = new Breuk[amountOfLines];
        for(int i=0; i<amountOfLines;i++){
            double numerator = (double) lezer.nextInt();
            double denominator = (double) lezer.nextInt();
            fractionArray[i].setNumerator(numerator); // HERE IT GOES WRONG, THE NULLPOINTEREXCEPTION IS HERE
            fractionArray[i].setDenominator(denominator);// THIS LINE SUFFERS AS WELL FROM THE NULLPOINTEREXCEPTION
            lezer.nextLine();
        }

    }

    public static int lengteBestand() throws FileNotFoundException{
        Scanner lezer = new Scanner(new File("breuken01.txt"));
        amountOfLines=0;
        while(lezer.hasNextLine()){
            amountOfLines++;
            lezer.nextLine();
        }
        return amountOfLines;
    }

}

The Breuk(is dutch for Fraction) class:

package labo2oef5;

public class Breuk {

    private double numerator, denominator;

    public Breuk() {
        numerator = 0;
        denominator = 1;
    }

    public Breuk(double numerator, double denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    @Override
    public String toString() {
        String breuk = numerator + "/" + denominator;
        if (numerator == 0) {
            breuk = "0";
        }
        return breuk;
    }

    private static double grootsteGemeneDeler(double a, double b) {
        if (a < 0 || b < 0) {
            return grootsteGemeneDeler(Math.abs(a), Math.abs(b));
        }
        if (b == 0) {
            return a;
        }
        if (a < b) {
            return grootsteGemeneDeler(b, a);
        }
        if (a % b != 0) {
            return grootsteGemeneDeler(b, a % b);
        } else {
            return b;
        }
    }

    public String normaliseer() {

        double numerator2 = numerator / (grootsteGemeneDeler(numerator, denominator));
        double denominator2 = denominator / (grootsteGemeneDeler(numerator, denominator));
        return numerator2 + "/" + denominator2;
    }

    public double decimaleVorm() {
        double decim = (double) numerator / denominator;
        System.out.println(decim);
        return decim;
    }

    public boolean isGroterDan(Breuk br) {
        if (this.decimaleVorm() > br.decimaleVorm()) {
            return true;
        } else {
            return false;
        }
    }

    public void vermeerderBreuk(Breuk br) {
        this.numerator = this.numerator * br.denominator + this.denominator * br.numerator;
        this.denominator = this.denominator * br.denominator;
    }

    public void produkt(Breuk br, Breuk breu) {
        numerator = br.numerator * breu.numerator;
        denominator = br.denominator * breu.denominator;
    }

    public void setNumerator(double numerator) {
        this.numerator = numerator;
    }

    public void setDenominator(double denominator) {
        this.denominator = denominator;
    }

    public double getNumerator() {
        return numerator;
    }

    public double getDenominator(){
        return denominator;
    }
} 

Both answers are the right answer ,I just had to add this code underneath their answer as well: if (lezer.hasNextLine()) { lezer.nextLine(); }

Upvotes: 0

Views: 267

Answers (2)

Sylwek
Sylwek

Reputation: 866

fractionArray[i] is null, so it throws NullPointerException. I think that you should do it like that:

fractionArray[i] = new Breuk();
fractionArray[i].setNumerator(numerator);
fractionArray[i].setDenominator(denominator);

Upvotes: 4

duffymo
duffymo

Reputation: 308938

Try this:

public static void readFractions() throws FileNotFoundException{
    Scanner lezer = new Scanner(new File("breuken01.txt"));
    fractionArray = new Breuk[amountOfLines];
    for(int i=0; i<amountOfLines;i++){
        double numerator = (double) lezer.nextInt();
        double denominator = (double) lezer.nextInt();
        fractionArray[i] = new Breuk(numerator, denominator);
        // Creating an array of references leaves them all null until you point them to something non-null by calling new
        lezer.nextLine();
    }

}

Upvotes: 3

Related Questions