I made the code look lke one below but it gives me the following error
Your code caused an Exception:
java.lang.NullPointerException
Comments:
The code should handle the possibility that the input array is null.
view source
print?
Code:01 class StudentSubmission {
02 int indexOfLargestNumber(int[ ] inputNumbers) {
03 int startIndex =0;
04 int max = inputNumbers[startIndex];
05 int indexOfMax = startIndex;
06
07 for (int index = startIndex + 1; index< inputNumbers.length; index++)
08 {
09 if (inputNumbers ==null|| inputNumbers.length == 0)
10 {
11 indexOfMax = -1;
12 return indexOfMax;
13
14 }
15 else
16 {
17 for ( index = startIndex + 1; index< inputNumbers.length; index++)
18 {
19 if (inputNumbers[index]>max)
20 {
21 max = inputNumbers[index];
22 indexOfMax = index;
23 }
24
25 }
26
27 }
28
29 }
30 return indexOfMax;
31 }
32 }
Below is the whole question
This method takes an array of numbers as input and returns the index of the largest number in that array. If the largest number appears multiple times, ties are settled by returning the index of its first appearance. For instance, given [4, 2, 7, 1, 7, 3] as input, the method returns 2 (the index of the first appearance of 7 in the array).
The following is the method prototype for the method:
int indexOfLargestNumber(int[ ] inputNumbers)
If inputNumbers is null or empty, the method should return -1 to indicate an error condition.
Please help I am really struggling here
