Search code examples
unsafeunchecked-exception

unchecked or unsafe operations even after specifying type


I get the following warning

Note: com.......\BeerSelect.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

I have specified the type as well.. I would like to know the reason rather than using the @SupressWarning option.

I checked the other threads as well What causes javac to issue the "uses unchecked or unsafe operations" warning

The below is the piece of code..

    package com.example.web;

    import com.example.model.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class BeerSelect extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws     IOException, ServletException{
    String c = request.getParameter("color");
    BeerExpert be = new BeerExpert();
    List<String> result = be.getBrands(c);
    ServletContext sc = this.getServletConfig.getServletContext();


    request.setAttribute("styles",result);
    RequestDispatcher view =    getServletConfig.getServletContext().getRequestDispathcer("result.jsp");

    view.forward(request,response); 
}

}

Any sort of help is appreciated.. Thanks


Solution

  • Try using

    Iterator<String>
    

    instead of Iterator This happens because the Iterator interface is generified. And BTW its not an error but just a warning. hope this helps

    Hi, I've slightly modified your code and it should get compiled with no warnings:

    public class BeerSelect extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws     IOException, ServletException{
        String c = request.getParameter("color");
        BeerExpert be = new BeerExpert();
        List<String> result = be.getBrands(c);
        ServletContext sc = this.getServletConfig().getServletContext();
        request.setAttribute("styles",result);
        RequestDispatcher view = sc.getRequestDispatcher("result.jsp");
    
        view.forward(request,response);
    }
    

    }

    I think the only way to get such a warning here is from BeerExpert class, if for example it returns List and not List like this:

    public class BeerExpert {
    
    public List getBrands(String color) {
        return new ArrayList(); // really dumb implementation, you should have something smarter here
    }
    

    }

    I suggest you to check this out and if needed to change to :

    public class BeerExpert {
    
    public List<String> getBrands(String color) {
        return new ArrayList<String>();
    }
    

    }

    Hope this helps