# Null Pointer Exception

I am trying to write a program that takes in data from a text file which is just a list of random integers. I have three classes, one for Fractions, one for sorting them and one to run everything.

Somehow all the data is getting lost in the array "A" and I am getting a null pointer exception in the Fractions class (which was given to us so there is nothing wrong with it) at the line "int a = frac1.getNumerator();"

Fractions Class
[code]import java.util.*;

public class Fraction implements Comparator
{
//the numerator of the fraction
private int numerator;

//the denominator of the fraction
private int denominator;

//---------------------------------------

// Constructors

//-----------------------------------------

//creates a fraction 0/1

public Fraction()
{
this(0,1);
}

//Creates a fraction number/1

public Fraction(int number)
{
this(number,1);
}

//Creates a fraction numerator/denominator passed as parameters

public Fraction(int numerator, int denominator)
{
//One can not have a zero denominator
if (denominator == 0)
{
System.out.println("Fatal Error: Can not set denominator to zero");
System.exit(1);
}

//Make the numerator "store" the sign
if (denominator < 0)
{
numerator = numerator * (-1);
denominator *= -1;
}

this.numerator = numerator;
this.denominator = denominator;

reduce();
}

//-----------------------------------------------------------
// Returns the sum of the passed Fraction to the Fraction
//------------------------------------------------------------

{
int a,b,c,d;

a = this.numerator;
b = this.denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*d + b*c,b*d);

}

//-----------------------------------------------------------
// Returns the difference of the passed Fraction to the Fraction
//------------------------------------------------------------

public Fraction subtract(Fraction frac)
{
int a,b,c,d;

a = numerator;
b = denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*d - b*c,b*d);

}

//-----------------------------------------------------------
// Returns the product of the passed Fraction to the Fraction
//------------------------------------------------------------

public Fraction multiply(Fraction frac)
{
int a,b,c,d;

a = numerator;
b = denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*c,b*d);

}

//-----------------------------------------------------------
// Returns the division of the passed Fraction to the Fraction
//------------------------------------------------------------
public Fraction divide(Fraction frac)
{
return multiply(frac.reciprocal());
}

//------------------------------------------------------
//Returns the reciprocal of this fraction
//----------------------------------------------------
public Fraction reciprocal()
{
return new Fraction(denominator,numerator);
}

//-----------------------------------------------
//compares this fraction with the passed Fraction for equality.
//If they are equal, it returns true, else false
//-------------------------------------------------------
public boolean equals(Fraction frac)
{
return (numerator == frac.getNumerator()
&& denominator == frac.getDenominator());
}
//------------------------------------------------
//Does the assignment of values without changing addresses
//-------------------------------------------------------
public void assigns(Fraction frac)
{
numerator = frac.getNumerator();
denominator = frac.getDenominator();
}

//------------------------------------------------------
//This adds 1 to the fraction, so that -3/2 becomes -1/2
//------------------------------------------------------
public void increment()
{
numerator += Math.abs(denominator);
}

//implements the compare method of Comparator: it returns a
//negative number if the first Fraction sent is smaller than the second
//fraction sent, zero if equal, and a positive number if first Fraction sent
//is larger than the second Fraction sent

public int compare(Fraction frac1,Fraction frac2)
{
int a = frac1.getNumerator();
int b = frac1.getDenominator();
int c = frac2.getNumerator();
int d = frac2.getDenominator();

return a*d - b*c;
}

public int getNumerator()
{
return numerator;
}

//------------------------------------------------
//returns the value of denominator
//-------------------------------------------------------

public int getDenominator()
{
return denominator;
}

//---------------------------------------------------------
//Returns the String representation of this Fraction
//--------------------------------------------------------
public String toString()
{
String result;

if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}

//-------------------------------------------------------
//This reduces the fraction by dividing both the numerator and the
//denominator by their greatest common divisor.
//-----------------------------------------------------------

private void reduce()
{
if (numerator != 0)
{
int common = gcd(Math.abs(numerator),denominator);

numerator = numerator / common;
denominator = denominator / common;
}
else
denominator = 1;
}

//---------------------------------------------------------------
//This computes and returns the greatest common divisor of the two
//positive parameters. It uses Eculid's algorithm
//----------------------------------------------------------------

private int gcd(int m, int n)
{
int r;

//Make sure that m is the smaller of the two numbers
if (m > n)
{
r = m;
m = n;
n = r;
}

r = n % m;

while(r != 0)
{
n = m;
m = r;
r = n % m;
}

return m;
}
}[/code]
Sort Fractions Class
[code]import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.InputMismatchException;
import javax.swing.filechooser.FileFilter;
import java.util.Scanner;

public class SortFractions
{
Fraction [] A; //array of Fractions to be sorted
int n =0;
int MAX=0;
File file;
Scanner console = new Scanner(System.in);

public SortFractions(File input,int m) throws FileNotFoundException
{
//--------------------------------------------------------
// Assign the values to file and MAX; and call the appropriate methods
//-------------------------------------------------------
file = new File("D:\DePaul\hw5\data.txt");
file = input;
MAX = m;
getArraySize();
selectionSort();
}

{
String line;
Scanner scanner;
int numerator,denominator;
int i = 0;
A = new Fraction[MAX];

try
{
scanner = new Scanner(file);
//A = new Fraction[MAX];
numerator = scanner.nextInt();
denominator = scanner.nextInt();

// create the scanner object and
// create the array of Fractions, A, of size MAX

while(scanner.hasNextInt())
{
A[i] = new Fraction(numerator,denominator);
numerator = scanner.nextInt();
denominator = scanner.nextInt();
i++;
}
scanner.close();
}
catch(FileNotFoundException exception)
{
System.exit(0);
}
//--------------------------------------------------------
// do a catch for other possible IO exceptions here
//
//-------------------------------------------------------

}

private void selectionSort()
{
//A = new Fraction[MAX];
int minIndex=0,i=0,smallestIndex=0;
Fraction temp = new Fraction();
Fraction f = new Fraction();
n = A.length; //size of the array
for(i = 0; i < n - 1; i++)
{
smallestIndex = i;
for(minIndex = i+1; minIndex < n; minIndex++)
{
if(f.compare(A[minIndex],A[smallestIndex])<0)
{
smallestIndex = minIndex;
temp = A[smallestIndex];
}
A[i] = temp;
}
}

//------------------------------------------------------
//
// Do the selectionSort here. Just follow the model given
// on page 859-861 of the text. Note that the three lines of
// swapping the values using temp is OUTSIDE the inner loop.
//
//------------------------------------------------------

}

public int getArraySize()
{
return n;
}

public Fraction[] getSortedArray()
{
return A;
}

public void printArray()
{
for(int i=0; i<n; i++)
{
System.out.println(A[i]);
}
}
//It is just a for loop and
// inside the for loop you just a System.out.println statement.
}[/code]
Test Class
[code]import java.io.*;
/**
* Write a description of class testSort here.
*
* @version (a version number or a date)
*/
public class testSort
{
public static void main(String args[])
{
int max=20;
File file = new File(args[0]);
SortFractions test = null;
try
{
test = new SortFractions(file,max);
test.printArray();
}
catch(FileNotFoundException e)
{
System.exit(0);
}
catch(IOException e)
{
System.out.println(e);
System.exit(0);
}

int n = test.getArraySize();
Fraction[] A = new Fraction[max];
A = test.getSortedArray();

test.printArray();
test.getArraySize();
}

}[/code]

Thanks for taking a look for me.

• Hey,
Basically the program tries to process the entire array, of Fractions, but the array probably has some "empty" elements, ie. Fractions whose "value" is null.
See attached code with display statements.
Note that the location of where the input numbers are read was also moved to prevent "bypassing" a set of numbers. Possibly a reason the array had null elements?

regards, se52