Results 1 to 4 of 4
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default Inconsistencies dealing with null

    I have this piece of code that is driving me crazy. I don't understand how my map can contain two values on one line and on the very next line be null without me doing anything to it. Here is the code with the output

    Java Code:
    //puts the key and value together in the map
    System.out.println(groups.get(xx)+" : "+rolesList.size());
    for(int ii = 0; ii<rolesList.size(); ii++){
    	System.out.println(groups.get(xx)+" ...:..."+rolesList.get(ii));
    }
    System.out.println(groupRoleInfoMap.size());	
    //Displays this
    //Corporate Engineering Technology : 2
    //Corporate Engineering Technology ...:...Developer
    //Corporate Engineering Technology ...:...Drafter
    
    groupRoleInfoMap.put(groups.get(xx),rolesList);
    List<String> testList = groupRoleInfoMap.get(groups.get(xx));
    for(int ii = 0; ii < testList.size(); ii++){
    	System.out.println(testList.get(ii));
    }
    //Displays
    //	Developer
    //	Drafter
    
    try{
            System.out.println(groupRoleInfoMap.get(xx)); //null
    	groupRoleInfoMap.get(xx).addAll(rolesList);
    }catch(NullPointerException npe){
    	System.out.println("NPE NPE: "+npe.getLocalizedMessage());
    	//this gets caught
    }
    What am I missing that groupRoleInfoMap.get() starts returning null values between the first loop and the try block?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You are using different keys
    Java Code:
    groupRoleInfoMap.get(groups.get(xx));
    ...
    System.out.println(groupRoleInfoMap.get(xx));
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Thank you for spotting that. I was mistakenly using my map like a list. It has been changed to
    Java Code:
    groupRoleInfoMap.get((groups.get(xx)))
    One peculiar thing I noticed was that when I would hit that print statement during execution it would display properly
    Java Code:
    groupRoleInfoMap.get(Corporate Engineering Technology) = [Developer, Drafter]
    groupRoleInfoMap.get(Rig Solutions.RS Mechanical.Mud Pumps) = [Drafter]
    however when I would return the map and try
    Java Code:
    System.out.println(groupRoleInfoMap.get(Corporate Engineering Technology));
    System.out.println(groupRoleInfoMap.get(Rig Solutions.RS Mechanical.Mud Pumps));
    it would only return
    Java Code:
    [Drafter]
    Turns out using rolesList.clear() wasn't good enough to actually clear my list and had to be replaced by rolesList = new ArrayList<String>();

    Does the clear method not "Removes all of the elements from this list (optional operation). This list will be empty after this call returns (unless it throws an exception)." because no exception was being thrown here?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by xcallmejudasx View Post
    It has been changed to
    Java Code:
    groupRoleInfoMap.get((groups.get(xx)))
    No need for double brackets.

    Quote Originally Posted by xcallmejudasx View Post
    Turns out using rolesList.clear() wasn't good enough to actually clear my list and had to be replaced by rolesList = new ArrayList<String>();
    I don't see any clear() in your code.

    You might have less trouble if you wrote a Multimap class yourself to handle the adding to mapped lists. This would keep your collection-handling code separate and you'd be less likely to make mistakes.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Dealing with iReport 3.0.0
    By HotEvilGirl in forum New To Java
    Replies: 6
    Last Post: 09-11-2009, 11:32 AM
  2. Null
    By leapinlizard in forum New To Java
    Replies: 4
    Last Post: 04-29-2009, 11:29 PM
  3. Null Error
    By scoleman123 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  4. Null value
    By Michael in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:25 AM
  5. What is NULL
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 04:55 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
  •