Link to home
Start Free TrialLog in
Avatar of vishey68
vishey68

asked on

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

Avatar of a_b
a_b

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().
Avatar of vishey68

ASKER

if you go to the end of my attached code i am populating it
i am sorry, i have a getter method defined in there. i have a getter method

HashMap getOutHashMap()

return outHashMap

Have you checked what vales do you get in the singleton when the getOutHashMap() is called?
no, i have not checked that, but i will have to use the same logic and that would be null
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
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
i think i understand the mistake i have done, i will correct it test it and then post it
ASKER CERTIFIED SOLUTION
Avatar of avdej
avdej
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial