Hashmap values are null

I have defined a singleton class in which i am populating hashmap. I would like to use the value in another class. However i am getting null values. I have attached the code below.

The class which is calling the singleton class

HashMap k = new HashMap();  

                 k = PageMapper.getOutHashmap();  

                 if (k == null) {

                              System.out.println("k is null");

                 }

                 else {

                              dCacheHashMap.putAll(k);    

                 }

The class which is having the method is below
private PageMapper() throws NAPInitializationException
 
      {
 
             init();
 
      }
 
 
 
      public static PageMapper getInstance() throws NAPInitializationException
 
      {
 
            if(m_instance == null)
 
            {
 
                  // this block is synchronized to avoid false initialization.
 
                  synchronized (PageMapper.class) 
 
                  {
 
                        if(m_instance == null)
 
                        {
 
                              m_instance = new PageMapper();
 
                        }
 
                  }
 
            }
 
            return m_instance;
 
      }
 
      private void init() throws wAPInitializationException
 
      {
 
            boolean TRACE_ON              = JLogManager.isTraceOn();
 
            final String LOGGING_METHOD = "init";
 
            HashMap PageMapMap = new HashMap();
 
            try
 
            {
         
   Document document = (Document) loader.getJconfigContent(PAGE_MAPPER_FILE);
          if (document == null)
 
          {
 
             HashMap outHashmap     =  new HashMap();    
 
                  HashMap inHashMap      =  new HashMap();
 
            
 
                  Element e = document.getDocumentElement();
 
                  NodeList markets = document.getElementsByTagName("Market");
 
                  
 
                for (int i = 0; i < markets.getLength(); i++) {
 
                  
 
                  Element market = (Element)markets.item(i);
 
                        String mktName = market.getAttribute("name");
 
                  System.out.println("The market attribute  name is  " +mktName);
 
                  
 
                  NodeList pageMappings = market.getElementsByTagName("page-mapping");
 
                        for (int j = 0; j < pageMappings.getLength(); j++ ) {
 
                              Element pageMapping    =    (Element)pageMappings.item(j);
 
                              String reqPageName     =    pageMapping.getAttribute("request-handler-page-name");
 
                              String configPageName  =    pageMapping.getAttribute("struts-config-page-name");
 
                              System.out.println("The request-handler-page-name attribute  name is  " + reqPageName);
 
                              System.out.println("The struts-config-page-name attribute name is  " + configPageName);
 
                              if(reqPageName != null && configPageName != null){
 
                                    System.out.println("inhashmap is " + inHashMap.toString());
 
                                    inHashMap.put(reqPageName,configPageName);
 
                                    System.out.println("inhashmap is " + inHashMap.toString());
 
                                    
 
                              }
 
                              
 
                        }
 
                        
 
                        outHashmap.put(mktName,inHashMap);              
 
                        
 
                }
 
The value of k in the called class is always null

Open in new window

vishey68Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Have you tried debugging through the getInstance code and see what is the value of the hashmap? Also, I cannot see the code for the getOutHashManp().
0
vishey68Author Commented:
if you go to the end of my attached code i am populating it
0
vishey68Author Commented:
i am sorry, i have a getter method defined in there. i have a getter method

HashMap getOutHashMap()

return outHashMap

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

a_bCommented:
Have you checked what vales do you get in the singleton when the getOutHashMap() is called?
0
vishey68Author Commented:
no, i have not checked that, but i will have to use the same logic and that would be null
0
avdejCommented:
The code above has a lot of design inconsistencies and bugs. It won't work.

I suggest:
1. you post the WHOLE code of your PageMapper class
2. you tell exactly how you want to use the class from outside (e.g. which getters should it have)
2. I'll will make the code runnable
3. then you can modify it to meet your particular needs

Avdej
0
vishey68Author Commented:
I have only hidden only a little bit of details that is mostly to do with logging details only.Rest everything is there.
 i have populated the outhashmap with the values from XML.
I then call this singleton pageMapper class from the code given ouside. I thought that by having a getter method for outhashmap i can get it outside.

Please let me know if u need any more info
0
vishey68Author Commented:
i think i understand the mistake i have done, i will correct it test it and then post it
0
avdejCommented:
Well. Then consider at first lines 61 and 62. You've declared both references outHashmap and inHashmap as local variables. That means they exist only within the enclosing block, i.e. between lines 58 nand 120.

You have to:
1. declare the both maps as private INSTANCE variables
2. use these references within init() to populate the maps with values
3. make the maps accessible from outside by means of public getters

Avdej
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.