-2

I having a error with my hibernate project. It gives me a NullPointerException when counts the size of my list(null) cause it breaks when doing the Query and doesnt set the 2 parameters that i pass, when i doint just with one parameter works perfectly.

Here is my code:

AlertsHelper

public List getAlertsList(int level, String Process) 
{
    List<Dtalerts> list = null;
    try 
    {
            if(!this.session.isOpen()){
                this.iniciarConexion();
            }
        Query q = session.createQuery("from Dtalerts as dtalerts where  dtalerts.level = :level AND dtalerts.Process = :Process");
        //q.setParameter("Fh_alert", Fh_alert);
        q.setParameter("level", level);
        q.setParameter("Process", Process);
        list = (List<Dtalerts>) q.list();
    } catch (Exception ex) 
    {
        System.out.println("Excepcion " + ex);
        ex.printStackTrace();
    }
    return list;
}

Jsp

<%
    if(request.getParameter("level")!=null){
    int level = Integer.parseInt(request.getParameter("level"));
    String Process = request.getParameter("Process");
    AlertsHelper helper = new AlertsHelper();
    List alertList = helper.getAlertsList(level, Process);
    out.print("<table border='1'>");
    out.print("<tr><th>Level</th><th>Process</th></tr>");
    for(int i = 0; i <alertList.size(); i++){
    Dtalerts alerts = (Dtalerts) alertList.get(i);
    Id = alerts.getId();
    level = alerts.getLevel();
    Process = alerts.getProcess();
    String Description = alerts.getDescription();
    out.print("<td><a href='javascript:mostrarDatos(" + level + ",\"" + Process + ")'>Seleccionar</a></td>");
    out.print("<tr>");
    out.print("<td>" + level + "</td>");
    out.print("<td>" + Process + "</td>");
    out.print("<td>" + Description + "</td>");
    out.print("<td>" + Id + "</td>");
    out.print("</tr>");
    }
    out.print("</table>");
    }else{
    out.print("introduce un id");
    }%>
    </form>
Tony
  • 618
  • 12
  • 27
  • Use some other query parameter name then Process may be process or Process1 because you already have a class named Process – Aman Chhabra Jun 25 '18 at 08:01

1 Answers1

0

Firstly, prevent the NPE in the code. You should avoid returning null list from getAlertsList() by initializing the list with empty list:

List<Dtalerts> list = new ArrayList<>();

and fill the list with result like this :

list.addAll((List<Dtalerts>) q.list())

or put a null check on the return value in JSP file before running loop.

Rest is about correcting your Query. If you are not able to solve the query, please post the exception that you are getting.

raviiii1
  • 936
  • 8
  • 24