Retrieving map value with key

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1109 Views - Last Post: 30 June 2020 - 08:16 AM Rate Topic: -----

#1 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Retrieving map value with key

Posted 22 June 2020 - 02:52 PM

Hi guy, i need help, I am trying to retrieve the value from Map but after retrieving it, The program is throwing NullPointerException. i dont know where i got things wrong.

here is the code

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FriendMap<T extends PersonalFriends> {
    private String userName;
    Map<String, ArrayList<PersonalFriends>> friendMap= new HashMap<String, ArrayList<PersonalFriends>>();
    ArrayList<PersonalFriends> friendRecord;

    public FriendMap(String userName) {
        this.userName = userName;
        this.friendRecord = new ArrayList<PersonalFriends>();
    }

    public boolean createMap(String username, ArrayList<PersonalFriends> myFriend){
        friendMap.put(username, myFriend);
        return true;
    }

    public ArrayList<PersonalFriends> returnFriendsInMap(String username){
        String key = findFriendsKey(username);
        for(Map.Entry<String, ArrayList<PersonalFriends>> data : friendMap.entrySet()){
           if(key.equals(data.getKey())){
               return data.getValue();
           }
        }
        return null;
    }


    public String findFriendsKey(String username){
        for(String key: friendMap.keySet()){
         if(friendMap.containsKey(username)){
                return key;
            }
        }
        return null;
    }

    


}


and to retrieve the value i have this:

import java.util.ArrayList;

public class LoginDisplayPage<T extends PersonalFriends> {
    private String username;
    private ArrayList<T> myFriendList;

    public LoginDisplayPage(String username) {
        this.username = username;
         this.myFriendList = new ArrayList<>();
    }

    public void headerDisplay(String username) {
        FriendMap fm = new FriendMap(username);
        myFriendList = fm.returnFriendsInMap(username);

            int count = 0;
            for (int i = 0; i < myFriendList.size(); i++) {
                System.out.println(myFriendList.get(i));
                count++;
            }
            System.out.println(" \t\t\t\t\t\t FriendList: " + count + " Friend(s)");
            System.out.println("***--***--***--***--***--***--***--***--***--***");
            System.out.println("");

            String key = fm.findFriendsKey(username);
            System.out.println("The key : " + key);


        }
    }


I dont know if this line of code is correct
public ArrayList<PersonalFriends> returnFriendsInMap(String username){
        String key = findFriendsKey(username);
        for(Map.Entry<String, ArrayList<PersonalFriends>> data : friendMap.entrySet()){
           if(key.equals(data.getKey())){
               return data.getValue();
           }
        }
        return null;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving map value with key

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 22 June 2020 - 03:10 PM

Quote

throwing NullPointerException

Can you copy the full text of the error message and paste it here?
The message has the line number where the exception happened. Look at that line and find the variable with the null value and then backtrack in the code to see why.
Was This Post Helpful? 1
  • +
  • -

#3 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Retrieving map value with key

Posted 22 June 2020 - 03:14 PM

View PostNormR, on 22 June 2020 - 03:10 PM, said:

Quote

throwing NullPointerException

Can you copy the full text of the error message and paste it here?
The message has the line number where the exception happened. Look at that line and find the variable with the null value and then backtrack in the code to see why.



Here is the error Texts

Quote

***--***--***-xxx-***--***--***

Welcome! SEYI,
Exception in thread "main" java.lang.NullPointerException
at Academy.learn.LoginDisplayPage.headerDisplay(LoginDisplayPage.java:19)
at Academy.learn.Gregistration.loginDisplay(Gregistration.java:111)
at Academy.learn.Main.displayHome(Main.java:136)
at Academy.learn.Main.main(Main.java:31)



Quote

at Academy.learn.LoginDisplayPage.headerDisplay(LoginDisplayPage.java:19)

 myFriendList = fm.returnFriendsInMap(username);


This post has been edited by seyidesh: 22 June 2020 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 22 June 2020 - 03:23 PM

I don't see the cause yet, but I do see a potential problem.
There are two variables with the same name: username.
One declared on line 4 and one on line 12. That can be a cause of errors and confusion. It is better to use unique names to prevent confusion.
Was This Post Helpful? 0
  • +
  • -

#5 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Retrieving map value with key

Posted 22 June 2020 - 03:30 PM

View PostNormR, on 22 June 2020 - 03:23 PM, said:

I don't see the cause yet, but I do see a potential problem.
There are two variables with the same name: username.
One declared on line 4 and one on line 12. That can be a cause of errors and confusion. It is better to use unique names to prevent confusion.



One declared on line 4 and one on line 12.
The two variables are different, One is declared in a Method and the other is a Global Variable, There is no confusion in that. My concern is that method that I wrote to retrieve and return the value of the MAP ( which is an ArrayList ), I don't know if my write with that code
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 22 June 2020 - 03:48 PM

It is easy to think you are using the class variable when it is a local variable.

How are you trying to debug the code? Add some print statements before line 19 that print out the values of the variables used on line 19: fm and username
Was This Post Helpful? 0
  • +
  • -

#7 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: Retrieving map value with key

Posted 23 June 2020 - 02:59 AM

At a guess, your findFriendsKey method is returning null - which you don't test for in returnFriendsInMap.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: Retrieving map value with key

Posted 23 June 2020 - 03:26 AM

I have to say your code is confusing. That's because it's confused. You need to revisit it or this confusion will probably grow. Just two observations (there are probably more)

a. createMap doesn't do anything of the sort, it puts into a map
b. findFriedsKey doesn't do anything useful. You can just call get on the Map to find out if it contains the key, though at this stage i'm not sure why you'd want to do that
c.

Quote

for(Map.Entry<String, ArrayList<PersonalFriends>> data : 

Why are you iterating the map? If you have the key you should be using it in the normal way
You need to look at what maps do, how they work and how they are used

This post has been edited by g00se: 23 June 2020 - 03:30 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#9 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Retrieving map value with key

Posted 23 June 2020 - 04:31 AM

View Postg00se, on 23 June 2020 - 03:26 AM, said:

I have to say your code is confusing. That's because it's confused. You need to revisit it or this confusion will probably grow. Just two observations (there are probably more)

a. createMap doesn't do anything of the sort, it puts into a map
b. findFriedsKey doesn't do anything useful. You can just call get on the Map to find out if it contains the key, though at this stage i'm not sure why you'd want to do that
c.

Quote

for(Map.Entry<String, ArrayList<PersonalFriends>> data : 

Why are you iterating the map? If you have the key you should be using it in the normal way
You need to look at what maps do, how they work and how they are used



Thanks for your observations, let me briefly tell you what i was trying to achieved, i working on a mini project that create and save mutual friends. Am trying create Map that store Arraylist of of personal friends using username as Key Map<username, Arraylist<PersonalFriends>> ,

I only call CreateMap when i have retrieved all details of friends from an Arraylist that stored all registered friends. The details is use to create new arraylist that stored in the Map. FindFriendKey is use to retrieve the key which is Username and the key is use to retrieve value of friends that is stored in Arraylist inside Map.


I believe you now have ideas of what am trying to do. I will appreciate your views if there are other ways to achieve this task.

At the moment, even though my Map show created with Boolean check, i noticed, there are nothing stored in the Map.
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 23 June 2020 - 04:45 AM

Can you make a small, complete program that compiles and executes for testing?

Did you try debugging with a print statement as I suggested? What was printed?
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: Retrieving map value with key

Posted 23 June 2020 - 06:56 AM

Let's take one simple use case - get List<PersonalFriend> for user 'U', i.e. get a list of personal friends (not sure what other kind of friend there is) of the user called 'U'. Implementation:

map.get("U") - if null there are none.

Another use case: add a list of friends. Implementation:

a. create list and add the friends
b. map.get("U") - if null put list else
c. if not null, addAll to existing list

Note neither requires iteration or any other kind of 'querying' of the map - just using it in the normal way
Was This Post Helpful? 0
  • +
  • -

#12 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Retrieving map value with key

Posted 23 June 2020 - 07:48 AM

View Postg00se, on 23 June 2020 - 06:56 AM, said:

Let's take one simple use case - get List<PersonalFriend> for user 'U', i.e. get a list of personal friends (not sure what other kind of friend there is) of the user called 'U'. Implementation:

map.get("U") - if null there are none.

Another use case: add a list of friends. Implementation:

a. create list and add the friends
b. map.get("U") - if null put list else
c. if not null, addAll to existing list

Note neither requires iteration or any other kind of 'querying' of the map - just using it in the normal way


I did not understand your view, You are not getting the picture:

Here is the picture:
Initially, after registration, all friends are stored in List<PersonalFriend>. Every registered Friend have access to its own personal Page where he can now add his own personalFriend, His personal friend will be store in a Map<username, ArrayList<PersonalFriend>>. The map is meant to Hold individual Personal friends in a ArrayList<PersonalFriend>.

here is the view

Inside the Map
Friend A - with username A --- ArrayList<PersonalFriend> of all his own friend
Friend B - with username B --- ArrayList<PersonalFriend> of all his own friend
Friend C - with username C --- ArrayList<PersonalFriend> of all his own friend

when a user login to view his page, he should be able to use is username as key to retrieve the values of his friends stored in the Map. as ArrayList.
Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 23 June 2020 - 07:58 AM

Why aren't these explanations included in the source file as comments so anyone reading the code will understand?

Did you try adding the print statement? What was printed?
What compiler warnings and error messages do you get? Please copy and paste it here.
Was This Post Helpful? 0
  • +
  • -

#14 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Retrieving map value with key

Posted 23 June 2020 - 08:05 AM

View PostNormR, on 23 June 2020 - 07:58 AM, said:

Why aren't these explanations included in the source file as comments so anyone reading the code will understand?

Did you try adding the print statement? What was printed?
What compiler warnings and error messages do you get? Please copy and paste it here.


Why aren't these explanations included in the source file


I was trying to attach the sources file ZIP but failing, I have about 7 classes in the project.

The project is running fine but the Map session is not working fine.
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Retrieving map value with key

Posted 23 June 2020 - 08:18 AM

Quote

the Map session is not working

Can you make a small, simpler version with hardcoded values where needed that will compile and execute for testing. There is no need for any GUI. The required user input should be replaced by statements to load the Lists and Maps as needed for the test. The program should execute and show the problem without any user input required.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3