0

I have a homework question I need help with

We have been given a text file containing one word per line, of a story. We need to read this file into an array, perform a sort on the array and then perform a binary search.

The task also says I'll need to use an overload method, but I'm unsure where

I have a bubble sort, that I've tested on a small array of characters which works

public static void bubbleV1String(String[]numbers)
{
    for(int i = 0; i < numbers.length-1; i++)
    {
        for(int j = 0; j < numbers.length-1; j++)
        {
            if(numbers[j] .compareTo(numbers[j+1])>0)
            {
                String temp = numbers[j+1];
                numbers[j+1] = numbers[j];
                numbers[j] = temp;
            }
        }
    }
}`

And my binary search which I've tested on the same small array

    public static String binarySearch(int[] numbers, int wanted)
{
    ArrayUtilities.bucketSort(numbers);

    int left = 0;
    int right = numbers.length-1;

    while(left <= right)
    {
        int middle = (left+right)/2;

        if (numbers[middle] == wanted)
        {
            return (wanted + " was found at position " + middle);
        }

        else if(numbers[middle] > wanted)
        {
            right = middle - 1;
        }

        else
        {
            left = middle + 1;
        }

    }
    return wanted + " was not found";
}

Here is my code in an app class to read in a file and sort it

        String[] myArray = new String[100000];
    int index = 0;

    File text = new File("threebears.txt");

    try {
        Scanner scan = new Scanner(text);

        while(scan.hasNextLine() && index < 100000)
        {
            myArray[index] = scan.nextLine();
            index++;
        }
        scan.close();
    } catch (IOException e) {
        System.out.println("Problem with file");
        e.printStackTrace();
    }

    ArrayUtilities.bubbleV1String(myArray);
    try {
        FileWriter outFile = new FileWriter("sorted1.txt");
        PrintWriter out = new PrintWriter(outFile);

        for(String item : myArray)
        {
            out.println(item);

        }
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

When I go to run the code, I get a null pointer exception and the following message

 Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.compareTo(Unknown Source)
at parrayutilities.ArrayUtilities.bubbleV1String(ArrayUtilities.java:129)
at parrayutilities.binarySearchApp.main(binarySearchApp.java:32)

Line 129 refers to this line of code of my bubblesort

                if(numbers[j] .compareTo(numbers[j+1])>0)

And line 32 refers to the piece of code where I call the bubblesort

ArrayUtilities.bubbleV1String(myArray);

Does anyone know why I'm getting a null pointer exception when I've tested the bubblesort on a small string array? I'm thinking possibly something to do with the overloaded method mentioned earlier but I'm not sure

Thanks

Ali97
  • 91
  • 8
  • `numbers[j]` most likely is `null`. You created an array with length 100000 but I doubt the file has that many lines. Thus instead of looping from 0 to `numbers.length - 1` you should loop from 0 to `index - 1`. It would be better to use a `List` in that case but I have the feeling that this is one thing your teacher is trying to hint at (maybe at some later point in time). – Thomas Apr 02 '19 at 09:11
  • An easier way to fix your code would be to create a [copy of `myArray`](https://stackoverflow.com/questions/5785745/make-copy-of-an-array) which has the correct length, i.e. an array of length `index` - after reading the file. Then you replace `myArray` with that copy. – Thomas Apr 02 '19 at 09:14
  • @Thomas You answered my question, for the most part. I didn't realise the array needed to be created to the same number of lines that the text file contained. I checked the number of lines and remember overhearing the teacher mention about the specific number of lines in class, although this wasn't put in the task sheet, so after a few days I had forgotten about what she had said Once I corrected the array length the code ran fine Thank you – Ali97 Apr 02 '19 at 09:19
  • If you'd like to add an answer, I can select it as the best answer and close the question – Ali97 Apr 02 '19 at 09:21
  • If allowed in your homework, you may use the java.nio.file API, which makes it easier reading files with the Files.readAllLines(Path) method. This returns a list. You may convert the list into an array using the streams API: myList.stream().toArray(String[]::new). – Amadán Apr 02 '19 at 09:29
  • No problem, I'll add an answer. :) – Thomas Apr 02 '19 at 09:43

3 Answers3

1

It seems that you have some null values in your numbers array. Try to debug your code (or just print array's content) and verify what you have there. Hard to tell anything not knowing what is in your input file.

1

Method overloading is when multiple functions have the same name but different parameters.

e.g. (taken from wikipedia - function overloading)

// volume of a cube
int volume(const int s)
{
    return s*s*s;
}

// volume of a cylinder
double volume(const double r, const int h)
{
    return 3.1415926*r*r*static_cast<double>(h);
}

Regarding your null pointer exception, you've created an array of size 100000, but it's likely you haven't read in enough information to fill that size. Therefore some of the array is empty when you try to access it. There are multiple ways you can go about this, off the top of my head that includes array lists, dynamic arrays or even moving the contents of the array to another one, once you know the size of the contents (however this is inefficient).

Natalia Sharon
  • 216
  • 2
  • 17
1

You are creating an array of length 100000 and fill the lines as they are read. Initially all elements will be null and after reading the file quite a number of them is likely to still be null. Thus when you sort the array numbers[j] will eventually be a null element and thus calling compareTo(...) on that will throw a NullPointerException.

To fix that you need to know where in the array the non-null part ends. You are already tracking the number of read lines in index so after reading the file that would be the index of the first null element.

Now you basically have 2 options:

  • Pass index to bubbleV1String() and do for(int i = 0; i < index-1; i++) etc.
  • Make a copy of the array after reading the lines and before sorting it:
    String[] copy = new String[index];
    StringSystem.arrayCopy(myArray,0,copy,0,index);
    //optional but it can make the rest of the code easier to handle: replace myArray with copy
    myArray = copy;

Finally you could also use a List<String> which would be better than using arrays but I assume that's covered by a future lesson.

Thomas
  • 87,414
  • 12
  • 119
  • 157