0

I'm running out of patience and needs this problem fixed. This program is intended to retrieve data from two text files as two string arrays, then use a mergesort algorithm to sort the results. My issue is during the conversion to an integer array. I return the array I created, and see that there is data stored. However, when running an loop and checking if any index is null, I find that the program believes them all to be null.

import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
public class MergeInventories
{
    public static File inv1 = new File("H:\\Senior Year\\CompSci\\Projects\\storeOneInv.txt");
    public static File inv2 = new File("H:\\Senior Year\\CompSci\\Projects\\storeTwoInv.txt");
    //the two text files I'm retrieving data from
    public static String[] store1; //string array in question
    public static String[] store2;

public static void header()
{
    System.out.println("Keenan Schmidt");
    System.out.println("AP Computer Science");
    System.out.println("Merge Inventories");
    System.out.println("...finally...");
}

public static void main() throws FileNotFoundException
{    
    header();
    readFiles(inv1,store1); //converts file to string array
    sort(); //converts string[] to int[]
    //System.out.print(readFiles(inv1,store1));
    //System.out.print(readFiles(inv2,store2);
}

public static String[] readFiles(File file, String[] store)
{

    try {

        Scanner scanner = new Scanner(file);
        int i = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner = new Scanner(file);
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            i++;
        }

        store = new String[i];
        i = 0;
        scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            store[i] = line;
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return store;
}

public static int[] sort()
{
    int[] items = new int[store1.length];
    for(int i = 0; i < store1.length; i++)
    {
        if(store1[i] != null) //this is the line where the error occurs
        {
            try{
                items[i] = Integer.parseInt(store1[i].replaceAll("[^0-9]"," "));
            } catch (NumberFormatException nfe) {};
        }
    }

    return items;
}

private void mergeSort(String[] arr1, String[] arr2)
{

}

private void merge(int low, int med, int hi)
{

}
}
keenween
  • 3
  • 2
  • So, what do you suggest I do with my `readFiles()` method in order to give `store1` other values, reference it in the method? Edit; in other words, how do I add values to the array globally? – keenween Mar 19 '15 at 15:04
  • I've converted my comment into an answer so I have more room to talk. – azurefrog Mar 19 '15 at 15:08

4 Answers4

2

You are getting a NullPointerException when trying to access store1 because you never give store1 a value other than null, because Java is pass-by-value.

You create a new array, but you only assign it to store, which is a local variable in readFiles(), and that assignment has no effect on the store1 variable.

You do return that value from your method, but you neglected to assign it in the invoking code.

Replace

readFiles(inv1,store1); //converts file to string array

with

store1 = readFiles(inv1,store1); //converts file to string array and saves it in store1

so that the created array is assigned to store1.

As dkatzel points out, this means that there is no longer any point in passing store1 into the method in the first place. It would be a good idea to follow his advice on cleaning up the method.

Community
  • 1
  • 1
azurefrog
  • 9,994
  • 7
  • 36
  • 51
2

As azurefrog mentions in a comment, Java arrays are pass by value (the reference to the array) so that when you reassign the store variable in the method, the original array you passed in doesn't get the new reference assignment.

Since you want to re-use this method multiple times to make different arrays, I would suggest making a new array everytime inside the method. No need to pass it in.

static String[] readFiles(File file){
    String[] store =null;
    //rest of method this same

}

Then in your calling code:

store1 = readFiles(inv1);
store2 = readFiles(inv2);
Community
  • 1
  • 1
dkatzel
  • 29,286
  • 2
  • 57
  • 64
0

You can use a List at first because the file could be of unknown size, then convert the List to an Array (using toArray), and then you will know the length to which you should initialize the int array and your code can proceed as expected.

tinker
  • 1,336
  • 10
  • 19
0

either change to this: store1 = readFiles(inv1,store1);

or in readFiles() use this.store1 instead

Rusheel Jain
  • 778
  • 5
  • 17