-1

I have been coding a basic method to take an array of strings, find the largest length and return a string array containing on values that are equal to the highest length of the array. I keep getting a null pointer exception and i am not sure why. The code is:

String[] allLongestStrings(String[] inputArray) {
    int compare = 0;
    int k = 0;
    String[] use = new String[20];
    for (int i = 0; i < inputArray.length; i++) {
        if (inputArray[i].length() > compare)
            compare = inputArray[i].length();

    }

    for (int j = 0; j < 20; j++) {
        if (inputArray[j].length() - compare == 0) {
            use[k] = inputArray[j];
            k++;
        }

    }
    return use;

}
  • What's the exact error with trace? And have you done any debugging? – Carcigenicate Nov 04 '17 at 00:41
  • You don't appear to ever populate the `use` array. – Carcigenicate Nov 04 '17 at 00:43
  • post the exception logs. May be your inputArray is null. – nits.kk Nov 04 '17 at 01:21
  • Exception in thread "main" java.lang.AssertionError: java.lang.reflect.InvocationTargetException at myCode._json_encode(file.java on line ?) at myCode.main(file.java on line ?) Caused by:java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce‌​ssorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe‌​thodAccessorImpl.jav‌​a:43) Caused by: java.lang.NullPointerException at myCode._json_encode(file.java on line ?) is what the console outputs – cmw2993 Nov 04 '17 at 01:28

2 Answers2

1
for (int j = 0; j < 20; j++) {
    if (inputArray[j].length() - compare == 0) {
        use[k] = inputArray[j];
        k++;
    }
}

This will only work if inputArray has at least 20 elements. In the code above, you're doing the correct thing: for (int i = 0; i < inputArray.length; i++). I think you just need to change this second for statement to be the lesser of 20 or the length of inputArray.

negacao
  • 1,244
  • 8
  • 17
  • I tried that, and it still gives the null pointer. – cmw2993 Nov 04 '17 at 01:08
  • Want to share your modifications here? – negacao Nov 04 '17 at 01:09
  • String[] allLongestStrings(String[] inputArray) { int compare = 0; int k = 0; String[] use = new String[20]; for(int i = 0; i < inputArray.length; i++){ if(inputArray[i].length() > compare) compare = inputArray[i].length(); } for(int j = 0; j < inputArray.length; j++){ if(inputArray[j].length() - compare == 0){ use[k] = inputArray[j]; k++; } } return use; } – cmw2993 Nov 04 '17 at 01:10
  • How many strings are in inputArray? – negacao Nov 04 '17 at 01:13
  • it varies, but no more than 20 will be used in the tests. – cmw2993 Nov 04 '17 at 01:15
  • I'm just not seeing it. Do you know what line you're getting the exception on? Do you have any sample data? – negacao Nov 04 '17 at 01:18
  • I know that the error is in the line if (inputArray[j].length() - compare == 0) when I delete that line and run the program, the null pointer goes away. – cmw2993 Nov 04 '17 at 01:23
0

inputArray doesn't contain 20 elements. Don't just throw out and hard code a length value for your the array you're going to return. Actually determine what the true length is going to be because you could be too low on that value and if your not then you could end up with a bunch of null elements.

If allowed use an ArrayList or String List object which doesn't require a preset length (size) instead of a 1D String Array which does require length initialization: List<String> list = new ArrayList<>();. You can simply just add to it with the List.add() method.

If you must use a Single Dimensional Array then use another for loop to determine how many string elements actually have the same length as what is held within the compare integer variable. Always iterate through the original array (inputArray). This for loop would be very much like your second for loop except you would increment a integer counter variable upon all elemental lengths that equal the value held within the compare variable.

Eliminate the formula in your if statement condition contained within your second for loop. I think (IMHO): if (inputArray[i].length() == compare) {...} should be sufficient, it's much easier on the eyes. ;)

Just a thought for pizazz....perhaps add the actual Array index to the string that is added to the 1D String array named used. Use a delimiter of some sort to separate the two.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22