Results 1 to 12 of 12
  1. #1
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default 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]+" ");
    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    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.

  3. #3
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Sorry. I'll try again:

    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]+" ");
    }
    
    }
    Error at this line: if(A[minIndex].compare(A[smallestIndex],A[minIndex]) > 0)
    java.lang.NullPointerException: null

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Indentation?
    Java Code:
    for (int i = 0; i < n; i++) {
        System.out.println("Array: " + A[i]);
    }
    What is the output of that loop?

  5. #5
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Now take a close look at the very first line of your selection sort method.

  7. #7
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Now take a close look at your readArray method. See if you can spot the problem without further hints from me.

  9. #9
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

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

  11. #11
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    So, you set var to 42 and then you pass 100, but 42 still prints because that is what var was originally set to?

  12. #12
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Finally got it:

    Java Code:
    private void readArray()
    {
    
    String line;
    Scanner scanner;
    int numerator,denominator;
    
    try
    {
    
    scanner = new Scanner(file);
    Fraction [] A = new Fraction[MAX];
    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

  1. Replies: 7
    Last Post: 11-01-2010, 02:04 PM
  2. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM
  3. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  4. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM
  5. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •