Results 1 to 7 of 7
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default null pointer when adding object into an array

    I have a null pointer while trying to add parameter into newList.
    I don't get why. Please help. Thank you for your time.

    Java Code:
    /**
    	 * Adds an item to the list.  This method assumes that the list is already
    	 * sorted in alphabetical order based on the names of the items in the list.
    	 * 
    	 * The new item will be inserted into the list in the appropriate place so
    	 * that the list will remain alphabetized by names.
    	 * 
    	 * In order to accomodate the new item, the internal array must be re-sized 
    	 * so that it is one unit larger than it was before the call to this method.
    	 *  
    	 * @param itemToAdd refers to a Listable item to be added to this list
    	 */
    	public void add(Listable itemToAdd) {
    
    		//Creating a new array with the size of the current list + 1
    		Listable[] newList = new Listable[this.items.length + 1];
    
    		int addIndex = 0;
    		for (int index = 0; index < this.items.length; index ++){
    			//If itemToAdd is lexicographically less than the first element in
    			//current list. Make first element = to itemToAdd and copy the rest
    			if (itemToAdd != null 
    					&&
    					(items[0].toString()).compareTo(itemToAdd.toString()) > 0){
    
    				newList[index + 1] = items[index];	
    			}
    			newList[0] = itemToAdd;
    		}
    		//If itemToAdd is lexicographically greater than the last element in
    		//current list. Copy all elements into newList and make the last element
    		//itemToAdd
    		addIndex = 0;
    		for (int index = 0; index < this.items.length; index ++){
    			if (items[this.items.length - 1].getName().toString().compareTo(itemToAdd.getName().toString()) < 0){
    				addIndex = items.length;
    				newList[index] = this.items[index];
    				newList[addIndex] = itemToAdd;
    			}
    		}
    		addIndex = 0;
    		for (int index = 0; index < this.items.length; index ++){
    			if (this.items[index].getName().compareTo(itemToAdd.toString()) > 0){
    				addIndex = index;
    			}
    			//The newList will add all the object from current list; it will be a copy
    			//up to the new index called addIndex - 1.
    			for (index = 0; index < addIndex; index ++){
    				newList[index] = this.items[index];
    			}
    			//At the new index, we will assign that index itemToAdd
    			newList[addIndex] = itemToAdd;
    			//we copy the rest of the current list into newList
    			for (index = addIndex + 1; index < newList.length; index ++){
    				newList[index] = items[index - 1];
    			}
    		}	
    		items = newList;
    	}

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: null pointer when adding object into an array

    Post the error here

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: null pointer when adding object into an array

    java.lang.NullPointerException

    at foodManagement.SortedListOfImmutables.add(SortedLi stOfImmutables.java:100)

    at PublicTests.testListSimpleAdd(PublicTests.java:21)


    hightling:
    Java Code:
     if (itemToAdd != null
                        &&
                        (items[0].toString()).compareTo(itemToAdd.toString()) > 0){

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: null pointer when adding object into an array

    Well, items[0] must be null..

  5. #5
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: null pointer when adding object into an array

    Please explain. I am trying to say the compare first element of the current list. I am not conceptually understanding why it would be null. Thanks.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,869
    Rep Power
    5

    Default Re: null pointer when adding object into an array

    its basic logic. There are two elements in that piece of code that can be null, and since you check that itemToAdd is not null, it MUST be items[0] that has a null-reference in it.

    And it is null, you can see that when you follow the flow of the code. Where is items[0] actually set to an object?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: null pointer when adding object into an array

    Ok thanks. So I will have to put a null check somewhere in the if. Thank you for your clarification and help.

Similar Threads

  1. string array returning null pointer.. any hints?
    By natdizzle in forum New To Java
    Replies: 6
    Last Post: 07-22-2013, 06:07 AM
  2. Multidimentional array-null pointer
    By applewood13 in forum Java Applets
    Replies: 11
    Last Post: 04-30-2011, 06:15 AM
  3. Null Pointer Exception in an Array of JCheckBoxes
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 04-02-2011, 04:50 PM
  4. Null Pointer Exception when adding items to ArrayList
    By ShadowCopy in forum New To Java
    Replies: 3
    Last Post: 08-27-2009, 09:23 AM
  5. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 10:51 PM

Tags for this Thread

Posting Permissions

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