我做一个JSP文件,左块是选择票的各种条件后,点提交,在右块,出现符合条件的票。
票的条件有year1,day1,month1,choose,点提交表单后,右块就用
String year=(String)request.getParameter("year1");
得到选择的条件来查询数据库,得到票。
但是我刚执行程序,就出现NullPointerException的错误,可能是我还没有点提交表单,
String year=(String)request.getParameter("year1");year得不到表单中的year1.是空值,出错。
那请问怎么办呢?????????????????
有错误我就进不去程序,进不去程序就不能提交表单
急!~!~!~!~!~!
主要程序如下:<p>系统票查询子系统</p>
  
<table align="center" border="1">
<tr>
<td>
<form name="fm3" action="index.jsp" method="get">
<table align="center" border="1">
<tr>
   <td>票的种类选择:</td>
   <td>
<select name="choose1"><option value="沈阳地区220KV电网系统图">沈阳地区220KV电网系统图</option>
<option value="沈阳地区66KV电网接线图">沈阳地区66KV电网接线图</option>
</select>
    </td>
</tr>
<tr>
   <td>使用年份:</td>
    <td> <select name="year1">
<option value="2002">2002年</option>  
<option value="2003">2003年</option>
<option value="2004">2004年</option>
<option value="2005">2005年</option>
<option value="2006">2006年</option>
<option value="2007">2007年</option>
<option value="2008">2008年</option>
<option value="2009">2009年</option>
</select>
    </td>
</tr>
<tr>
   <td>使用月份:</td>
    <td><select name="month1"><option value="1">1月</option>
<option value="2">2月</option>
<option value="3">3月</option>
<option value="4">4月</option>
<option value="5">5月</option>
<option value="6">6月</option>
<option value="7">7月</option>
<option value="8">8月</option>
<option value="9">9月</option>
<option value="10">10月</option>
<option value="11">11月</option>
<option value="12">12月</option></select></td>
</tr>
<tr>
   <td>使用日期:</td>
    <td><select name="day1"><option value="1">1日</option>
<option value="2">2日</option>
<option value="3">3日</option>
<option value="4">4日</option>
<option value="5">5日</option>
<option value="6">6日</option>
<option value="7">7日</option>
<option value="8">8日</option>
<option value="9">9日</option>
<option value="10">10日</option>
<option value="11">11日</option>
<option value="12">12日</option>
<option value="13">13日</option>
<option value="14">14日</option>
<option value="15">15日</option>
<option value="16">16日</option>
<option value="17">17日</option>
<option value="18">18日</option>
<option value="19">19日</option>
<option value="20">20日</option>
<option value="21">21日</option>
<option value="22">22日</option>
<option value="23">23日</option>
<option value="24">24日</option>
<option value="25">25日</option>
<option value="26">26日</option>
<option value="27">27日</option>
<option value="28">28日</option>
<option value="29">29日</option>
<option value="30">30日</option>
<option value="31">31日</option></select></td>
</tr>
</table>
<center><input type="submit" name="Submit1" value="提交选择信息"></center></form>
</td>
<%
String year=(String)request.getParameter("year1");
String choose=request.getParameter("choose1");
String month=(String)request.getParameter("month1");
String day=(String)request.getParameter("day1");
choose=new String(choose.getBytes("ISO8859_1"),"GBK");
choose=choose.trim();%>
<td>
<%if(choose.equals("沈阳地区220KV电网系统图")){String sql="select distinct(bianhao) from bds3_xtp1 where year1='"+year1+"' and month1='"+month1+"' and day1='"+day1+"'"; 
ResultSet rs1=conn.executeQuery(sql); 
%><%while(rs1.next()){%>
<form action="112.jsp" method="get">
<center>上表查询条件得到如下使用过的票:</center>
<table border=1 align="center">
<tr>
<td>编号:</td>
<td><input type="text" name="tjbh" value="<%=rs1.getString("bianhao")%>" size="8"></td>
<td><input type="submit" name="Submit" value="打开此票"></td>
</tr></table>
</form>
<%
}
}%>
<%if(choose.equals("沈阳地区66KV电网接线图")){String sql="select distinct(bianhao) from bds2_xtp1 where year1='"+year1+"' and month1='"+month1+"' and day1='"+day1+"'"; 
ResultSet rs=conn.executeQuery(sql); 
while(rs.next()){%>
<form action="113.jsp" method="get">
<center>上表查询条件得到如下使用过的票:</center>
<table border=1 align="center">
<tr>
<td>编号:</td>
<td><input type="text" name="tjbh" value="<%=rs.getString("bianhao")%>" size="8"></td>
<td><input type="submit" name="Submit" value="打开此票"></td>
</tr></table>
</form><%}%>
<%}}%></td>
</tr>
</table>

解决方案 »

  1.   

    不管你点没有都会执行,相信这个就是index.jsp,这样的话,最好先设一个isSubmited变量判断是否提交。
    if(isSubmited == XXX)
    {
    //原有代码
    }
    else
    {
    //不执行
    }
      

  2.   

    你是提交给了当前的页面,该页面在第一次显示的时候就去request中取得提交的参数了,当然是nullpoint了
      

  3.   

    String year=(String)request.getParameter("year1");
    取得的参数值很有可能是空值,如果是空值去执行 year.equals() 就会报空指针错误了。所以String year=(String)request.getParameter("year1");
    取得参数以后一定要加一个 if(year==null)year="";的判断而且条件判断的时候最好将 if(choose.equals("沈阳地区66KV电网接线图")) 
    改为 if("沈阳地区66KV电网接线图".equals(choose)),,,这样就算choose是空值也不会报空指针错误了。
      

  4.   

    你第一次打开index.jsp的时候,表单并没有提交,所以在request里并没有year1等参数,所以String year=(String)request.getParameter("year1");就一定是null了。你需要加一个判断,如果是null,则说明是第一次进来,这时可以只显示查询条件,不显示查询结果,如果不为null,则说明不是第一次进来,就没有问题了。
      

  5.   

    UP,主要是你一开始没有任何值时用request.getParameter("year1");year用得到null楼主可以在出错的地方打印就很明显了。
      

  6.   

    //将下面代码
    choose=new String(choose.getBytes("ISO8859_1"),"GBK");
    choose=choose.trim();修改为choose = choose!=null?choose.trim():"";
    choose=new String(choose.getBytes("ISO8859_1"),"GBK");
      

  7.   

    我也做过空值的判断,但是具体怎么做判断我也不是很清楚,能告诉我具体点吗????
    比如说if(year1==null) 然后执行什么else我的程序代码
    具体的格式不是很清楚。
      

  8.   

    我做的判断如下:
    <%String choose=request.getParameter("choose1");
    choose=choose!=null?choose.trim():"";
    choose=new String(choose.getBytes("ISO8859_1"),"GBK");%>
    <%if(request.getParameter("year1")==null||request.getParameter("year1").equals(""))
                 year1=2002;
      else
      year6=(String)request.getParameter("year1");
      if(request.getParameter("month1")==null||request.getParameter("month1").equals(""))
                 month1=1;
      else
      month6=(String)request.getParameter("month1");
      if(request.getParameter("day1")==null||request.getParameter("day1").equals(""))
                 day1=1;
      else
      day6=(String)request.getParameter("day1");
    %>但是还是出很多的错误,如下:
    Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    year1 cannot be resolvedAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    year6 cannot be resolvedAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    month1 cannot be resolvedAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    month6 cannot be resolvedAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    day1 cannot be resolvedAn error occurred at line: 193 in the jsp file: /index.jsp
    Generated servlet error:
    day6 cannot be resolvedAn error occurred at line: 214 in the jsp file: /index.jsp
    Generated servlet error:
    year6 cannot be resolvedAn error occurred at line: 214 in the jsp file: /index.jsp
    Generated servlet error:
    month6 cannot be resolvedAn error occurred at line: 214 in the jsp file: /index.jsp
    Generated servlet error:
    day6 cannot be resolvedAn error occurred at line: 239 in the jsp file: /index.jsp
    Generated servlet error:
    year6 cannot be resolvedAn error occurred at line: 239 in the jsp file: /index.jsp
    Generated servlet error:
    month6 cannot be resolvedAn error occurred at line: 239 in the jsp file: /index.jsp
    Generated servlet error:
    day6 cannot be resolved
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.8 logs.
      

  9.   

    大家看看我加这样的判断
    <%String choose=request.getParameter("choose1");
    String year6=(String)request.getParameter("year1");
    String month6=(String)request.getParameter("month1");
    String day6=(String)request.getParameter("day1");
    choose=choose!=null?choose.trim():"";
    choose=new String(choose.getBytes("ISO8859_1"),"GBK");%>
    <%if(request.getParameter("year6")==null||request.getParameter("year6").equals(""))
               {  year6="2006";}
      
     
      if(request.getParameter("month6")==null||request.getParameter("month6").equals(""))
      {           month6="3";}
     
      
      if(request.getParameter("day6")==null||request.getParameter("day6").equals(""))
                { day6="9";}
      if(request.getParameter("choose")==null||request.getParameter("choose").equals(""))
      {            choose="沈阳地区220KV电网系统图";}
      
      
    %>
    程序能进去了,但是无论怎么修改选择条件,提交表单后都是
    day6="9"
    choose="沈阳地区220KV电网系统图"
    month6="3";}
    上面的判断不就是没有提交表单是空的时候给他值以免出错误,但是我提交表单还是我设的值,不是我提交的值。
      

  10.   

    还有人把SERVLET写JSP上面阿
    太乱了,不看了
      

  11.   

    <%String choose=request.getParameter("choose1");
    String year6=(String)request.getParameter("year1");
    String month6=(String)request.getParameter("month1");
    String day6=(String)request.getParameter("day1");
    choose=choose!=null?choose.trim():"";
    choose=new String(choose.getBytes("ISO8859_1"),"GBK");
    if(year6==null||year6.equals(""))
               {  year6="2006";}
      
     
      if(month6==null||month6.equals(""))
      {           month6="3";}
     
      
      if(day6==null||day6.equals(""))
                { day6="9";}
      if(choose==null||choose.equals(""))
      {            choose="沈阳地区220KV电网系统图";}
      
      
    %>
      

  12.   

    <html> 
    <head> 
    <title> dddd</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    </head> 
    <body> 
    <form action="test.jsp" id=f1>
    <textarea  name="tt" ></textarea>
      <input type="button" name="button" value="button" onclick="kk()">
    <script>
    function kk(){
    alert(document.getElementById("tt").value);
    document.getElementById("f1").submit();
    }
    </script>   
    </form><%String s = request.getParameter("tt");
    if(s==null||s.equals("")){
    s="dddddd";
    }
    out.print("<br>"+s);
    %>
    </body> 
    </html> 
      

  13.   

    <html> 
    <head> 
    <title> dddd</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    </head> 
    <body> 
    <form action="test.jsp" id=f1>
    <select name="month1"><option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option></select>
      <input type="submit" name="submit" value="button" >
     
    </form><%String s = request.getParameter("month1");
    if(s==null||s.equals("")){
    s="dddddd";
    }
    out.print("<br>"+s);
    %>
    </body> 
    </html>