-4

I am running a code that takes a list of ints, and a list of strings, and separately increases the size of the array to the proper size, and then sorts the array in a different method while also then finding an instance of a duplicate. The code is fine up to the point where I run the methods where it sorts my array and looks for duplicates. I know what the right output should be, and there should be no duplicate found in intList and a duplicate found in wordList at index 45788. I have asked for help from others doing this same simple task, and have the same code as them. I have to be off somewhere but I cannot find where. I attached a photo of the two methods next to the output in the command prompt. Thanks for any help

import java.io.*;
import java.util.*;

public class Lab4
{
    static final int INITIAL_CAPACITY = 10;
    static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found

    public static void main (String[] args) throws Exception
    {
        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
        if (args.length < 1 )
        {
            System.out.println("\nusage: C:\\> java Lab4 <numbers file> <words filename>\n\n"); // i.e. C:\> java Lab4 10000ints.txt 172822words.txt
            System.exit(0);
        }

        String[] wordList = new String[INITIAL_CAPACITY];
        int[] intList  =  new int[INITIAL_CAPACITY];
        int wordCount = 0, intCount=0;
        Scanner intFile = new Scanner( new File(args[0]) );
        BufferedReader wordFile = new BufferedReader( new FileReader(args[1]) );

        // P R O C E S S   I N T   F I L E 
        while ( intFile.hasNextInt() ) // i.e. while there are more ints in the file
        {   
            if ( intCount == intList.length ) 
                intList = upSizeArr( intList );
            intList[intCount++] = intFile.nextInt();

        } //END WHILE intFile

        //close intfile
        intFile.close(); 

        //output text with variables
        System.out.format( "%s loaded into intList array. size=%d, count=%d\n",args[0],intList.length,intCount );

        int dupeIndex = indexOfFirstDupe( intList, intCount );

        if ( dupeIndex == NOT_FOUND )
        {
            System.out.format("No duplicate values found in intList\n");
        }
        else
        {
            System.out.format("First duplicate value in intList found at index %d\n",dupeIndex);
        }

        // P R O C E S S   S T R I N G    F I L E
        while ( wordFile.ready() ) // i.e. while there is another line (word) in the file
        {   
            if ( wordCount == wordList.length ) 
                wordList = upSizeArr( wordList );
            wordList[wordCount++] = wordFile.readLine();
        } //END WHILE wordFile

        //closing wordfile
        wordFile.close(); 

        //output text again with variables
        System.out.format( "%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount );

        dupeIndex = indexOfFirstDupe( wordList, wordCount );

        if ( dupeIndex == NOT_FOUND )
        {
            System.out.format("No duplicate values found in wordList\n");
        }
        else
        {
            System.out.format("First duplicate value in wordList found at index %d\n",dupeIndex);

        }
    }

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

    // method to double size of string array 

    static String[] upSizeArr( String[] fullArr )
    {
        int length = fullArr.length;

        //creating a new array of double size
        String[] upsizearr = new String[length*2];

            //this for loop assigns each old variable in fullArr
            //and assigns it to the new larger array, upsizearr
            for(int i = 0; i<length-1; i++)
            {
                upsizearr[i] = fullArr[i];
            }

        return upsizearr; 
    }


    // method to double size of int array 

    static int[] upSizeArr( int[] fullArr )
    {
        int length = fullArr.length;

        //creating new array of double size
        int[] upsizearr = new int[length*2];

            //this loop does the same as in upSizeArr method, 
            //assigning all values to new bigger array
            for(int i = 0; i<length-1; i++)
            {
                upsizearr[i] = fullArr[i];
            }

        return upsizearr; 
    }


    // use Arrays.sort() before scanning for dupe
    static int indexOfFirstDupe( int[] arr, int count )
    {       
        Arrays.sort(arr);
        int value = NOT_FOUND;

        for(int i = (arr.length - count); i < count; i++)
        {
            if(arr[i] == arr[i-1])
            {
                value = i;
                break;
            }
        }

        return value;
    }


    // use Array.sort() before scanning for dupe
    static int indexOfFirstDupe( String[] arr, int count )
    {       

        Arrays.sort(arr);
        int value = NOT_FOUND;

        for(int i = (arr.length - count); i < count; i++)
        {
            if(arr[i] == arr[i-1])
            {
                value = i;
                break;
            }
        }

        return value;   
    }

} // END CLASS

[cmd and sorting/finding dupe arrays] [code where errors occur]2

TimeToCode
  • 901
  • 2
  • 16
  • 34
  • If it it "run error" that it cannot be "code that works" and vice versa – Antoniossss Sep 30 '16 at 21:17
  • Add your code to the question instead the pictures. – TimeToCode Sep 30 '16 at 21:17
  • 2
    You showed the exception (in a picture instead of copy and pasting) and then didn't even show the line that it refers to (line 144, inside indexOfFirstDupe)... – jonhopkins Sep 30 '16 at 21:19
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stewart Sep 30 '16 at 21:21
  • @TimeToCode and jonhopkins, my mistake. I thought my screenshot captured more than it did. I pasted my code. – Justin Anderson Sep 30 '16 at 21:21
  • But which is line 144? There will you find your answer .... – Stewart Sep 30 '16 at 21:22
  • 144 is Arrays.sort(arr) in the last method. I tried changing it to Array, not Arrays, but with no success. – Justin Anderson Sep 30 '16 at 21:24
  • Turns out line 144 isn't all that helpful. Was hoping there would be more context around it. Best guess right now is that `arr` is `null` for whatever reason. You'll want to check that `wordList` in `main()` is being created correctly. – jonhopkins Sep 30 '16 at 21:26

3 Answers3

1

The console tells us the error is happening in indexOfFirstDupe() on Arrays.sort(). You have two methods with this name, but since this line runs fine we know the error happens after it: System.out.format( "%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount );

So the error is happing in the indexOfFirstDupe( String[] arr, int count )

I see you have imported java.util.*, so Arrays.sort() should be available and not causing the error. I would guess that 'arr' is null. Try printing arr to the console using System.out.println() before the line with Arrays.sort(arr). If it is null, that's your problem.

Brendan L
  • 1,436
  • 14
  • 13
1

Revised answer:

After looking into this more closely. It appears that the Arrays.sort() is to blame. One possibility is the way that the array sizes are increased by "upSizeArr". Or wordFile.readLine() is returning a null value when adding words to the wordList array. Whatever the reason is, the "countRunAndMakeAscending" error is mostly due to null values in the array to be sorted.

Others have ran into this issue as well:

Sorting an array of strings in Java

The suggestion is to use an ArrayList.

Alternatively, looping through the arrays and set any null values to a non-null value before sorting could resolve this. However, you'll have to determine a good non-null value candidate that would not corrupt the data set when checking for dupes in the "indexOfFirstDupe" method.

So, learning to use the ArrayList might be the easier route.

Leaving the old solution posted as it resolves a separate problem in your code.

Old answer:

It looks like your code is encountering a null value when looping through the word list array. While looking through your code, it appears that the issue might exists in the int list as well. So... a couple of things to correct. Change the way you are setting ints and words to the arrays by incrementing the intCount and wordCount variables last in the while-loops.

When loading ints into the intList...

// P R O C E S S   I N T   F I L E 
    while ( intFile.hasNextInt() ) // i.e. while there are more ints in the file
    {   
        if ( intCount == intList.length ) 
            intList = upSizeArr( intList );
        intList[intCount] = intFile.nextInt();
        intCount++;
    } //END WHILE intFile

When loading words into the wordList

    // P R O C E S S   S T R I N G    F I L E
    while ( wordFile.ready() ) // i.e. while there is another line (word) in the file
    {   
        if ( wordCount == wordList.length ) 
            wordList = upSizeArr( wordList );
        wordList[wordCount] = wordFile.readLine();
        wordCount++;
    } //END WHILE wordFile
Community
  • 1
  • 1
haoudoin
  • 151
  • 10
  • I tried this, and now have a "countRunAndMakeAscending" error. The same lines that were referenced before are still referenced again – Justin Anderson Sep 30 '16 at 21:51
  • That's the story with software programming, you fix one error and you move to the next... Without much context, it's hard to help you. i.e. Please post more information such as requested by others in this thread. – haoudoin Sep 30 '16 at 21:57
  • Please mark as answer if my post answered your original post... thanks! :) – haoudoin Sep 30 '16 at 21:58
  • I have the entirety of code pasted. I do not know what more information was asked recently if there was any. – Justin Anderson Sep 30 '16 at 22:03
  • 1
    Again, just guessing here since I have no way of confirming what the line numbers are in your code. But the error your encountering might be from the Arrays.sort() call as well. http://stackoverflow.com/questions/24175145/sorting-an-array-of-strings-in-java Might explain the new NullPointerException. So looping through your arrays and removing any null values before sorting might resolve that "countRunAndMakeAscending" error. – haoudoin Sep 30 '16 at 23:18
0

How to read exceptions:

Exception in thread "main" java.lang.NullPointerException

for beginner the only important part of this line is the last part ( java.lang.NullPointerException ) this is the type of your error. In this case you have some object with that is null and you trie to call a method on the null object.

at ...

at ...

at Lab4.IndexOfFirsDupe(Lab4.java:144)

at Lab4.main(Lab4.java:66)

this is the so called stack trace. It tells you where in your code the error is.

The entrys consist of three important informations: the class (lab4), the method (IndexOfFirstDupe) and the line in the code (line 144)

edit: I wrote this comment before the Code was added

Mein Name
  • 527
  • 3
  • 14