null pointer error

Discussions

Web tier: servlets, JSP, Web frameworks: null pointer error

  1. null pointer error (3 messages)

    i keep getting a null pointer. any insight?

    <%@ include file="dbconnection.jsp" %>
    <%-- Run SQL Query --%>
    <% String IPName;
       String sql;
       ResultSet rset = null;

        sql =
          "SELECT distinct left(IPName,1) AS IPTitle FROM IPs ";

        try {
          rset = stmt.executeQuery (sql);
        }
        catch (SQLException e) {
          out.println (e);
    return;
        }

        while (rset.next ())
        {
          IPName = rset.getString ("IPTitle");
    %>
    <%
    try {
            stmt.close();
            conn.close();
    } catch (Exception ex) {}
    %>

    <%= IPName %></a>
            |
    <%
    }
    %>

    </td>
      </tr>
    </table>
     
      
    <form method="POST" action="details.jsp">
    <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="3" width="727">
        <TR bgcolor="#FFFFCC">
          <TD width="220"><B>Information Provider</B></TD>
          
          <TD width="131"><B>Country</B></TD>
          <TD width="131"><b># of Publications</b></TD>
        </TR>
        <%-- Run SQL Query --%>
    <% int IPNumber;
       int NumberOfPubs;
       String CountryName;
       String sql2;

        sql2 =
          "SELECT IPs.IPNumber, IPs.IPName, IPs.CountryName, count(Titles.IPNumber) AS NumberOfPubs " +
          "FROM IPs, Titles " +
    "WHERE IPs.IPNumber = Titles.IPNumber " +
    "AND IPs.IPName LIKE " +
    "'" + request.getParameter("IPName") + "%'" +
    "GROUP BY IPs.IPNumber, IPs.IPName, IPs.CountryName ";

        try {
          rset = stmt.executeQuery (sql);
        }
        catch (SQLException e) {
          out.println (e);
    return;
        }

        while (rset.next ())
        {
          NumberOfPubs = rset.getInt ("NumberOfPubs");
    IPNumber = rset.getInt ("IPNumber");
    CountryName = rset.getString ("CountryName");
    IPName = rset.getString ("IPName");
    %>

          <tr>
            <td width="220"><%= IPName %>
              </a> </TD>
            <td width="219"><%= CountryName %> </TD>
            <td width="262"><%= NumberOfPubs %></TD>
          </tr>
         <%
    }
    %>
      </table>

    Threaded Messages (3)

  2. null pointer error[ Go to top ]

    Hi,

      Where is the definition of stmt? and the allocation of it?

    I mean you need a,

    Statement stmt;

    and you also need an,

    stmt = connection.createStatement();
  3. null pointer error[ Go to top ]

    make sure u check the var before u close them.

    do this.

    if (stmn != null) stmt.close();
    if (conn != null) conn.close();

    i've seen before if u try to close something that is null, u will get NPE.

    HTH.

    alex
  4. null pointer error[ Go to top ]

    Are you getting any rows back from the query? If not ResultSet will be null.

    Try:

    if (rset == null) {
        System.out.println("rset is null");
    } else {
        // Iterate through the result set
        while (rset.next ())
        {
          IPName = rset.getString ("IPTitle");
        }
    }