0

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

Community
  • 1
  • 1
  • Possible duplicate of [What causes javac to issue the "uses unchecked or unsafe operations" warning](https://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning) – user207421 Sep 02 '19 at 04:31

1 Answers1

0

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

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Thanks for the help.. But, I tried 'Iterator' but still I get the same warning.. Corrected it as warning in the question as well.. – user2202786 Mar 23 '13 at 17:50
  • Well, i guess you should post the warning here. It should point you on an exact line in the code that causes this warning – Mark Bramnik Mar 23 '13 at 19:15
  • Am getting the same warning message as above in the post.. Note: com.......\BeerSelect.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details – user2202786 Mar 23 '13 at 22:24
  • Can you post the source of BeerSelect.java file here then?r – Mark Bramnik Mar 24 '13 at 04:41
  • I had the specified the type in the body of the method getBrands(), but dint specify for the return type.. Thanks for all your help :) – user2202786 Mar 24 '13 at 18:43