Results 1 to 5 of 5
Thread: Arrays and Null inputs
- 07-20-2010, 05:32 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Arrays and Null inputs
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?
XML 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
- 07-20-2010, 05:45 PM #2
Please copy and paste the full contents of the error message here. One of the important items in the message is the line number of the source where the error occurred.t gives me the following error
Your code caused an Exception:
java.lang.NullPointerException
- 07-20-2010, 06:56 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
The java.lang.NullPointerException is caused when u try to access the first element of the empty array in line 04.
My suggestion would be to check for null array in the beg. of the function rather in the middle.
The alterred code:
Hope this Helps :)Java Code:int indexOfLargestNumber(int[] inputNumbers) { int indexOfMax; if (inputNumbers != null & inputNumbers.length != 0) { int startIndex = 0; int max = inputNumbers[startIndex]; indexOfMax = startIndex; for (int index = startIndex + 1; index < inputNumbers.length; index++) { for (index = startIndex + 1; index < inputNumbers.length; index++) { if (inputNumbers[index] > max) { max = inputNumbers[index]; indexOfMax = index; } } } } else { indexOfMax = -1; } return indexOfMax; }
- 07-20-2010, 07:02 PM #4
Why did you start a new thread?
Arrays and use of null or empty inputsMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-21-2010, 06:23 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, please stick with a one thread. Don't post the same question multiple times.
Similar Threads
-
Arrays and use of null or empty inputs
By Desmond in forum New To JavaReplies: 6Last Post: 07-27-2010, 04:54 PM -
Someone plz help... how to block inputs
By waklo99 in forum New To JavaReplies: 4Last Post: 03-15-2010, 06:44 AM -
if statement with multiple inputs?
By soc86 in forum New To JavaReplies: 3Last Post: 01-20-2009, 04:44 AM -
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05-18-2008, 04:22 PM -
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11-06-2007, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks