Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Error reading from Map in Session

Hi Everybody,

I have a problem when I try to read data from a hashmap in a session.

This is how I fill my list:

IPortalComponentSession session = ((IPortalComponentRequest)this.getRequest()).getComponentSession();	
		Map lijst = new HashMap();
		try
		{
			// get all ids from database
			int[] ids = sd.getAllSkillIDs();

			for(int i=0; i<6; i++)
			{
                                // get a skill from the database and insert the id and the name
				SkillBean s = sd.getSkillByID(ids<i>);
				lijst.put(String.valueOf(s.getId()), s.getName().toString());
			}
			session.putValue("lijst",lijst);
		}

This gives a runtime error when trying to read it in the jsp.

I'm sure that the object that is returned is filled and correct.

And when I create my list like this:

lijst.put("1","Value1");
lijst.put("2","Value2");

Then all the values contained in the list are printed.

This is my jsp

<%@taglib uri="tagLib" prefix="hbj"%>

<hbj:content id="myContext" >
  <hbj:page title="PageTitle">
 	
 	
	Skills in Database:<br/><br/>
	<% Map lijst = (java.util.HashMap)componentRequest.getComponentSession().getValue("lijst");
  	  	for(int i=1; i<=lijst.size();i++)
  	  	{
  	%>
  			<%=String.valueOf(i)%>  -=>     <%=lijst.get(String.valueOf(i)).toString() %>
  		<br/>
  	<%  		
  	  	}
  	%>

	</hbj:page>
</hbj:content>

Help me please

Thank you

Wouter

Former Member
Former Member replied

Hi Wouter Delellio ,

Your way of retriving value from the hashMap is not the right process as

At the time of putting values to the hashMap you are assigning key as follows :

// get all ids from database
int[] ids = sd.getAllSkillIDs();
 
for(int i=0; i<6; i++)
{
   // get a skill from the database and insert the id and the name
   SkillBean s = sd.getSkillByID(ids<i>);
   lijst.put(String.valueOf(s.getId()), s.getName().toString());
}

But in the jsp when you are retriving the data fom the hashMap the key that you have taken is as follows :

for(int i=1; i<=lijst.size();i++)

{

%>

<%=String.valueOf(i)%> -=> <%=lijst.get(String.valueOf(i)).toString() %>

<br/>

<%

}

where the key you habe used is the i whose value in the followinf code is 1,2......n

You need to wrte the code in the following manner where first you get the <b>Key</b> and then you get the value for that key . The code goes like this :

<%@taglib uri="tagLib" prefix="hbj"%>
 
<hbj:content id="myContext" >
  <hbj:page title="PageTitle">
 	
 	
Skills in Database:<br/><br/>
<% Map lijst = (java.util.HashMap)componentRequest.getComponentSession().getValue("lijst");
Set set = lijst.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
object obj = it.next();
String key = (String) obj.toString();
%>
  <%=String.valueOf(key)%>  -=>     <%=lijst.get(key)%>
  <br/>
<%  		
}
%>
 
	</hbj:page>
</hbj:content>

Now i think you will be able to get the value from the hash map.

If it works please do reqard with points.

Thanks

Ritushree Saha

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question