Hi, first of all I'm new at the forum I hope to help and be helped ;-).


I'm trying to display the values inside an ArrayList on a JSP. I execute a PreparedStatement and I put all the values inside an ArrayList<HashMap<String, Object>>. Once, I have all the values inside the ArrayList I put it in a HttpSession an redirect it to the JSP. In the JSP I try to display the values inside a row of the Arraylist but I get null all the times.

When I do "System.out.println(list.get(1)+"\n"+(list.get(1)).get(1));" I get:
{id=2, pass=muteki, user=muteki}
null

How can I get the values pass and user?

Thank you in advance

/************** JSP *************************/

ArrayList<HashMap<String, Object>> list = (ArrayList<HashMap<String, Object>>) session.getAttribute("list");
			
			System.out.println(list.get(1)+"\n"+(list.get(1)).get(1));
PreparedStatement pst = con
				.prepareStatement("select * from users where user=?");
		pst.setString(1, user);
		ResultSet rs = pst.executeQuery();
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		list = Conexion.resultSetToArrayList(rs);
HttpSession session = request.getSession();
		session.setAttribute("list", list);
		response.sendRedirect("/DataBase/dbuser.jsp");
public static ArrayList<HashMap<String, Object>> resultSetToArrayList(ResultSet rs) throws SQLException{
		 
		ResultSetMetaData md = rs.getMetaData();
		int columns = md.getColumnCount();
		ArrayList<HashMap<String, Object>> results = new ArrayList<HashMap<String, Object>>();

		while (rs.next()) {

			HashMap<String, Object> row = new HashMap<String, Object>();

			results.add(row);

			for (int i = 1; i <= columns; i++) {

				row.put(md.getColumnName(i), rs.getString(i));

			}

		}
		return results;

Recommended Answers

list.get(1) returns the HashMap that is in position 1 in the ArrayList.
When you print that HashMap you get its default output, which is a list of the key=value pairs in the HashMap
{id=2, pass=muteki, user=muteki}

list.get(1).get(1) gets the first HashMap then calls get(1) on that HashMap.
Now check …

Jump to Post

All 4 Replies

list.get(1) returns the HashMap that is in position 1 in the ArrayList.
When you print that HashMap you get its default output, which is a list of the key=value pairs in the HashMap
{id=2, pass=muteki, user=muteki}

list.get(1).get(1) gets the first HashMap then calls get(1) on that HashMap.
Now check out the JavaDoc for HashMap. Unlike ArrayList the parameter for HashMap's get is a key, not an integer position. So you are trying to retrieve the value whose key is the Integer 1, and there's no such key in the map, which is why it returns null.
You need to use something like
list.get(1).get("id")

It works!! Thank you very much

OK. Mark this thread as solved please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.