I am adding sort functionality to a successful Results(Invoice2.jpg) page.

I've created two drop-down list boxes to select the sort parameters:
One with column names, the other sort order (ASC or DESC)

I'm receiving a "Null Pointer Exception" error when I subit the sort.

The URL generated by the code when the error occurs:
http://localhost/jserv/Invoice2.jsp?order=Item&sort=ASC  The page is in the
"jserv" folder and is named: Invoice2.jsp
------------------------------------------
The error:
500 Internal Server Error /jserv/Invoice2.jsp:
java.lang.NullPointerException at
jrun__jserv__Invoice22ejsp13._jspService(jrun__jserv__Invoice22ejsp13.java:7
8) at allaire.jrun.jsp.HttpJSPServlet.service(../jsp/HttpJSPServlet.java:39)

I am able to move to the Page with success, the fields are populated and I
receive no errors.
Then, when I perform the sort, I receive the above error.

Here is my code:
----------------------------------------------------------------------------
---------------------
<%@page language="java" import="java.sql.*"%>
<%@ include file="../Connections/connBeachwear.jsp" %>
<%
String rsBeachwear__varCheckbox = "1";
if (request.getParameter ("valueCheckbox")    !=null)
{rsBeachwear__varCheckbox = (String)request.getParameter ("valueCheckbox")
;}
%>

<%
//NEW SORT VARIABLES:
String rsBeachwear__name = "ID";//default sort value
if (request.getParameter ("order") !=null) {rsBeachwear__name =
(String)request.getParameter ("order");}
%>
<%
String rsBeachwear__sort = "ASC";//default sort value
if (request.getParameter ("sort") !=null) {rsBeachwear__sort =
(String)request.getParameter ("sort");}
%>

<%
Driver DriverrsBeachwear =
(Driver)Class.forName(MM_connBeachwear_DRIVER).newInstance();
Connection ConnrsBeachwear =
DriverManager.getConnection(MM_connBeachwear_STRING,MM_connBeachwear_USERNAM
E,MM_connBeachwear_PASSWORD);
String chkValues[]=request.getParameterValues("valueCheckbox");
StringBuffer prepStr=new StringBuffer("SELECT ID, Item, Color, Size FROM
Beachwear WHERE ID=");
for(int x=0;x<chkValues.length;++x){
 prepStr.append( chkValues[x] );
 if((x+1)<chkValues.length){//There will be another iteration
  prepStr.append(" OR ID=");
  }//end if
 }//end for loop

//NEW SQL SORT CODE:
 prepStr.append(" ORDER BY '%" + rsBeachwear__name + "%' '%" +
rsBeachwear__sort + "%'");

//Sample:("SELECT ID, Item, Color, Size  FROM Beachwear  WHERE Color LIKE
'%" + rsBeachwear__varColor + "%'  ORDER BY Item");

PreparedStatement
StatementrsBeachwear=ConnrsBeachwear.prepareStatement(prepStr.toString());
ResultSet rsBeachwear = StatementrsBeachwear.executeQuery();
boolean rsBeachwear_isEmpty = !rsBeachwear.next();
boolean rsBeachwear_hasData = !rsBeachwear_isEmpty;
Object rsBeachwear_data;
int rsBeachwear_numRows = 0;
%>
<%
int Repeat1__numRows = -1;
int Repeat1__index = 0;
rsBeachwear_numRows += Repeat1__numRows;
%>
<title>Beachwear Title</title>
<body bgcolor="#FFFFFF">
<p>&nbsp;</p>
<p>&nbsp;
<p><br>
  INVOICE<br>
</p>
<%//FORM GET METHOD<%>
<form name="form1" method="get" action="Invoice2.jsp">
  <p><br>
</p>
  <% while ((rsBeachwear_hasData)&&(Repeat1__numRows-- != 0)) { %>
  <table width="75%" border="1">
    <tr>
      <td width="13%">ID:</td>
      <td width="87%"><%=(((rsBeachwear_data =
rsBeachwear.getObject("ID"))==null ||
rsBeachwear.wasNull())?"":rsBeachwear_data)%></td>
    </tr>
    <tr>
      <td width="13%">ITEM:</td>
      <td width="87%"><%=(((rsBeachwear_data =
rsBeachwear.getObject("Item"))==null ||
rsBeachwear.wasNull())?"":rsBeachwear_data)%></td>
    </tr>
    <tr>
      <td width="13%">COLOR:</td>
      <td width="87%"><%=(((rsBeachwear_data =
rsBeachwear.getObject("Color"))==null ||
rsBeachwear.wasNull())?"":rsBeachwear_data)%></td>
    </tr>
    <tr>
      <td width="13%">SIZE:</td>
      <td width="87%"><%=(((rsBeachwear_data =
rsBeachwear.getObject("Size"))==null ||
rsBeachwear.wasNull())?"":rsBeachwear_data)%></td>
    </tr>
  </table>
  <%
  Repeat1__index++;
  rsBeachwear_hasData = rsBeachwear.next();
}
%>
  <p>&nbsp; </p>
  <br>
  <table width="21%" border="1">
    <tr>
      <td width="30%">
        <div align="center">Parameter</div>
      </td>
      <td width="17%">
        <div align="center">1</div>
      </td>
      <td width="53%">
        <div align="center">2</div>
      </td>
    </tr>

<%//NEW PULL DOWN SORT LISTS %>
    <tr>
      <td width="30%">
        <div align="center">
          <input type="submit" value="Sort Now">
        </div>
      </td>
      <td width="17%">
        <div align="center">
          <select name="order" size="1">
            <option value="ID" selected>ID</option>
            <option value="Item">Item</option>
            <option value="Color">Color</option>
            <option value="Size">Size</option>
          </select>
        </div>
      </td>
      <td width="53%">
        <div align="center">
          <select name="sort" size="1">
            <option value="ASC" selected>Ascending</option>
            <option value="DESC">Descending</option>
          </select>
        </div>
      </td>
    </tr>

  </table>
  </form>
<%
rsBeachwear.close();
ConnrsBeachwear.close();
%>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to