Mismatch Type: Cannot convert from HashMap <Integer, Item> to HashMap <Integer, Item>

I am trying to use this piece of code in my JSP to iterate over a Hashmap. When jsp loads

    HashMap<Integer, Item> itemsMap = new HashMap<Integer, Item>();
    itemsMap = customer1.getItems();
      for(Map.Entry<Integer, Item> entry:itemsMap.entrySet()){
              Integer k=entry.getKey();
             Item item=entry.getValue();
      }

      

Here is the error that is displayed when trying to load jsp

    org.apache.jasper.JasperException: Unable to compile class for JSP: 
    An error occurred at line: 159 in the jsp file: /JSP/cart.jsp
    Type mismatch: cannot convert from HashMap<Integer,Item> to HashMap<Integer,Item>
    156: //   HashMap itemsMap = customer1.getItems();
    157: //          for(Iterator i = itemsMap.keySet().iterator();i.hasNext();){
    158:      HashMap<Integer, Item> itemsMap = new HashMap<Integer, Item>();
    159:      itemsMap = customer1.getItems();
    160:           for(Map.Entry<Integer, Item> entry:itemsMap.entrySet()){
    161:                   Integer k=entry.getKey();
    162:                   Item item=entry.getValue();

      

The same code worked fine when I used it in a servlet for the same purpose. Can anyone tell me what I am doing wrong here? Thank.

These are related classes:

public class Customer {
    private String name;
    private String address;
    // private ArrayList<Item> items = new ArrayList<Item>();
    private HashMap<Integer,Item> items=new HashMap<Integer,Item>();
    public Customer() {

    }
    public HashMap<Integer,Item> getItems(){ return items;}
}

public static class Item {
    private String name;
    private String skew;
    private String amount;
    public Item(String name, String skew, String amount) {
        this.name=name; this.skew=skew; this.amount=amount;
    }
}

      

+3


source to share


1 answer


Depending on the application server you are using, you can check the Java file that was generated from the JSP file. In Tomcat, for example, it is somewhere in the "work" directory tree. Some IDEs can also open it for you when you right-click the JSP file in a debugger session and choose something like "Show Servlet" from the context menu. Remember, there is no way for you to work when you are outside of a debugger session.

Once you open that file, you can check the import and everything else and why it won't compile.

Zircon user comment regarding "Item" type and possibly correct import of one of the JSPs into the JSP. I'm not worried about the lack of imports, but the design of your application. This is a very strong web development convention for separating business logic from display logic. In other words: there is no circumstance in which you have to have Java code in your JSP. All business logic (what your Java code needs to know) must be encapsulated in controllers and models, or, if you prefer, beans that you create from your JSPs. If you want to iterate over something, use <c:forEach >

similar provisions.



If you insist on using Java inside the JSP, then at least use the diamond operator: HashMap<Integer, Item> itemsMap = new HashMap<>();

So the issue would probably create a more enlightening compiler or runtime message.

If none of this helps, please do find the servlet file (.java file) generated by the application from your JSP and copy the import section in your question so that we know what type of element it is trying to access.

0


source







All Articles