Results 1 to 12 of 12
- 02-18-2011, 04:14 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Help needed: read array from file and then use it
Hello. First time poster and very new to java so thanks in advance. I've been staring at this for a while and would really appreciate the help. I have a class which reads in data from a file and stores it in an array. Then the array is sorted. I am not having any trouble reading or storing the array, but when I call my sort method, I am getting a null pointer exception. My array is not making it to the next method and I cannot seem to get it there. My code is below. Again, thanks in advance for any help.
import java.io.*;
import java.util.*;
public class SortFractions
{
Fraction [] A; //array of Fractions to be sorted
int n; //size of the array
final int MAX;
File file;
public SortFractions(File input,int m)
{
file = input;
MAX = m;
readArray();
selectionSort();
}
private void readArray()
{
String line;
Scanner scanner;
int numerator,denominator;
try
{
scanner = new Scanner(file);
Fraction [] A = new Fraction[MAX];
try
{
while(scanner.hasNext())
{
numerator = scanner.nextInt();
denominator = scanner.nextInt();
A[n] = new Fraction(numerator, denominator);
n++;
}//end of input reads
scanner.close();
}
catch(InputMismatchException e)
{
}
}
catch(FileNotFoundException exception)
{
System.out.println("The file " + file.getAbsolutePath() + "was not found.");
System.exit(0);
}
}
private void selectionSort()
{
A = new Fraction[MAX];
System.out.println("n is equal to: " +n);
for (int i = 0; i < n; i++)
System.out.println("Array: " + A[i]);
int index;
int smallestIndex;
int minIndex;
Fraction temp;
for (index = 0; index < n; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < n; minIndex++)
if(A[minIndex].compare(A[smallestIndex],A[minIndex]) > 0)
smallestIndex = minIndex;
temp = A[smallestIndex];
A[smallestIndex] = A[index];
A[index] = temp;
System.out.print(A[index]+" ");
}
}
- 02-18-2011, 04:23 AM #2
Since it is your first post..
When posting code use code tags. Place [ code ] before and [ /code ] after your code (without the spaces).
If you get an error message copy and paste the entire exact error message and indicate on which line it occurs.
- 02-18-2011, 04:29 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Sorry. I'll try again:
Error at this line: if(A[minIndex].compare(A[smallestIndex],A[minIndex]) > 0)Java Code:import java.io.*; import java.util.*; public class SortFractions { Fraction [] A; //array of Fractions to be sorted int n; //size of the array final int MAX; File file; public SortFractions(File input,int m) { file = input; MAX = m; readArray(); selectionSort(); } private void readArray() { String line; Scanner scanner; int numerator,denominator; try { scanner = new Scanner(file); Fraction [] A = new Fraction[MAX]; try { while(scanner.hasNext()) { numerator = scanner.nextInt(); denominator = scanner.nextInt(); A[n] = new Fraction(numerator, denominator); n++; }//end of input reads scanner.close(); } catch(InputMismatchException e) { } } catch(FileNotFoundException exception) { System.out.println("The file " + file.getAbsolutePath() + "was not found."); System.exit(0); } } private void selectionSort() { A = new Fraction[MAX]; System.out.println("n is equal to: " +n); for (int i = 0; i < n; i++) System.out.println("Array: " + A[i]); int index; int smallestIndex; int minIndex; Fraction temp; for (index = 0; index < n; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < n; minIndex++) if(A[minIndex].compare(A[smallestIndex],A[minIndex]) > 0) smallestIndex = minIndex; temp = A[smallestIndex]; A[smallestIndex] = A[index]; A[index] = temp; System.out.print(A[index]+" "); } }
java.lang.NullPointerException: null
- 02-18-2011, 04:37 AM #4
Indentation?
What is the output of that loop?Java Code:for (int i = 0; i < n; i++) { System.out.println("Array: " + A[i]); }
- 02-18-2011, 04:40 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
That print statement is just to test the output. I put it in to see if it would pull in the array. The output is null. The stored array does not make it to the method. What really confuses me is that my test of n, the size of the array does work and returns the correct number for the file I am testing.
- 02-18-2011, 04:44 AM #6
Now take a close look at the very first line of your selection sort method.
- 02-18-2011, 04:52 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Ah, yes, I'm creating a new array and not feeding anything into it. If I delete that, I get the null pointer exception at my test print statement. My array from readArray is still not feeding in.
- 02-18-2011, 04:56 AM #8
Now take a close look at your readArray method. See if you can spot the problem without further hints from me.
- 02-18-2011, 05:09 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Should I somehow be setting A as filled once my while loop is done running? This is the step that I just cannot figure out. When I combined my read and sort into one method, it worked just fine. I know that the problem is that my array A is not being read into the other method. I just don't know how to get it there.
- 02-18-2011, 05:16 AM #10
Perhaps an example might highlight what you doing wrong.
Java Code:class Foo { int var = 42; public void change(int n) { int var = n; } public void display() { System.out.println(var); } public static void main(String[] args) { Foo f = new Foo(); f.change(100); f.display(); } }
- 02-18-2011, 05:37 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
So, you set var to 42 and then you pass 100, but 42 still prints because that is what var was originally set to?
- 02-18-2011, 01:02 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Finally got it:
Fraction [] A = new Fraction[MAX];Java Code:private void readArray() { String line; Scanner scanner; int numerator,denominator; try { scanner = new Scanner(file); Fraction [] A = new Fraction[MAX];
should be A = new Fraction[MAX];
I was creating a new one. I think I get it. At least it works.
Thank you very much for your help. I really appreciate your time.
Similar Threads
-
Read text file into array instead of being hard coded in the script.
By biirdo in forum New To JavaReplies: 7Last Post: 11-01-2010, 02:04 PM -
Read File into 2d array
By almjodla in forum New To JavaReplies: 8Last Post: 03-23-2010, 02:55 PM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06-10-2008, 03:53 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks