Results 1 to 5 of 5
  1. #1
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    t gives me the following error

    Your code caused an Exception:
    java.lang.NullPointerException
    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.

  3. #3
    Viki is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default

    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:
    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;
        }
    Hope this Helps :)

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Why did you start a new thread?
    Arrays and use of null or empty inputs
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Arrays and use of null or empty inputs
    By Desmond in forum New To Java
    Replies: 6
    Last Post: 07-27-2010, 04:54 PM
  2. Someone plz help... how to block inputs
    By waklo99 in forum New To Java
    Replies: 4
    Last Post: 03-15-2010, 06:44 AM
  3. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM
  4. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •