cancel
Showing results for 
Search instead for 
Did you mean: 

HashMap(global variable) is not working in Message mapping

Former Member
0 Kudos

HI, In my outbound ORDERS map I've a situation that i've to use java HashMAp.  I tried hard but didn't work so to track down the issue I tried like this. And this is what i need also.

I declared the hasmap in the functions tab(ie.,global variable)


HasMap Test = new HasMap();

I created an UDF and i've the below code:

_________________________________

String Temp = "PI  ";
for(int i=0; i<2;i++)
{
Test.put(i,temp+i);
}

importantTrace.addWarning.("SIZE = " + Test.size());

for(int j=0; j<2;j++)
{
Test.get(j);
importantTrace.addWarning(Test.get(j));
}

________________________________________
at the start of the map in one of the link i've used this UDF and seeing the result as expected in the printf(addWarning) statement.

Result : SIZE = 2
             PI 0
             PI 1 

My situation is whatever i stored in my HashMap i must use in someother link/area down in the map.

so i created another UDF and added the  below code

________________________________________________
importantTrace.addWarning.("SIZE :: " + Test.size());
for(int j=0; j<2;j++)
{
Test.get(j,temp+j);
importantTrace.addWarning(Test.get(j));
}

________________________________________________


Result: SIZE :: 0

Since the size if zero i'm not getting the value which i stored in the HashMap.


Can you please help me on this issue ?

FYI: I've used  simple global variable like this it works fine.

Thanks,

Accepted Solutions (0)

Answers (5)

Answers (5)

mpothuganti
Explorer
0 Kudos

Everything is correct but small parallax error

Mistake with "HashMap" spelling

you used "HasMap" without "h"

Ryan-Crosby
Active Contributor
0 Kudos

Hi Rajkumar,

This would appear to be an order of operations issue in your mapping.  You stated that you have defined a global HashMap object in your mapping that is constructed with the statement:

HashMap Test = new HashMap();

Since the object Test most certainly has global scope (otherwise you'd get a NullPointerException) then the only other possibility is that in your mapping the second UDF is executing before the first UDF.  In that scenario the size of Test will still be zero and you'd get the result that you are seeing.  You need to focus on the order in which the items are executing at runtime.


Regards,

Ryan Crosby

anupam_ghosh2
Active Contributor
0 Kudos

Hi Rajkumar,

                   I feel  in your second UDF I feel the code should be

________________________________________________
importantTrace.addWarning.("SIZE :: " + Test.size());
for(int j=0; j<2;j++)
{
Test.get(j);
importantTrace.addWarning(Test.get(j));
}

________________________________________________

instead of

________________________________________________
importantTrace.addWarning.("SIZE :: " + Test.size());
for(int j=0; j<2;j++)
{
Test.get(j,temp+j);
importantTrace.addWarning(Test.get(j));
}

________________________________________________

Regards

Anupam

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Good catch.. Plus this would give compile error.  get(object 0b) method  is having one parameter.

Former Member
0 Kudos

Yes. That is the cut and paste error while creating this quesiton. Technically we can not have 2 parameters in the get method. In the 2nd UDF i've  Test.get(j); only.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Rajkumar,

                    Please try using simple global variable in UDF1 and access the variable in UDF 2.

If you are able to do so then similar method of storage has be applied for hashmap objects too.

Please look into this thread , this shows separete treatment for hashmap http://scn.sap.com/thread/1860541

I have not this myself, but the thread shows that type cast to string object might be necessary.

Regards

Anupam

Former Member
0 Kudos

Hi.

You can try with these UDFs to set and get a HashMap in GlobalContainer.

    public static void registerMap(String mapName[], String mapKeys[], String mapValues[], ResultList result, Container container) {

        GlobalContainer gc = container.getGlobalContainer();

        HashMap hm = new HashMap();

        for(int i = 0; i < mapKeys.length; i++)

            if(!hm.containsKey(mapKeys[i]))

                hm.put(mapKeys[i], mapValues[i]);

        gc.setParameter(mapName[0], hm);

    }

    public static String getFromMap(String mapName, String key, Container container) {

        GlobalContainer gc = container.getGlobalContainer();

        Object hm = gc.getParameter(mapName);

        Object value = null;

        if(hm != null)

            value = ((HashMap)hm).get(key);

        if(value != null)

            return value.toString();

        else

            return "";

    }

Regards

Lucho

anupam_ghosh2
Active Contributor
0 Kudos

Hi Rajkumar,

                  The values that you are trying to store in hasmap objects  has to typecasted to String before storage. If you look at Lucho's code above he is also putting String objects into hashmap. Please could you try this once.

Regards

Anupam

MichalKrawczyk
Active Contributor
0 Kudos

Hi,

>>>I declared the hasmap in the functions tab(ie.,global variable)

in Attributes and Methods ? (just checking)

Regards,

Michal Krawczyk

Former Member
0 Kudos

Yes Michal.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

The problem seems weird. I believe you are creating global variable in the function library -> functions and java areas -> Attributes and Methods. Is that right? If not, Please try it.

Former Member
0 Kudos

Hi.

How do you store the HashMap in Global Variable?