我现在有一个jsp文件,调用了javabean 从数据库里取出数据,jsp页面可以正常显示数据库数据,可是在我点击提交按钮以后,就出现了如下错误:
java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:363)
at java.lang.Long.parseLong(Long.java:452)
at org.apache.jsp.fsurveyContent$jsp._jspService(fsurveyContent$jsp.java:122)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ApplicationFilterChain.java:247)
......
long 类型我就在jsp页面里调用了parseLong方法,要是它出错,jsp页面就不应该能够显示出数据库的内容的,因为long类型是作为一个条件传给Bean进行查询的,为什么在点击按钮后不调用script反而出现这种错呢?请高手帮帮忙,谢谢了!在线等
QuerySurveyBean.java 所用到的代码如下:
public void setSurveyNum(Long num)
{
surveyNum=num;
}
public StringBuffer getSurveyInfo()
{
StringBuffer surveyInfo=new StringBuffer();
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
conn=dbConn();//连接数据库
String sql="select Title,Vote_class,Vote_title,Vote_count from SYS_STATASSESS_INFO as a,SYS_VOTE_INFO as b where a.Stat_id="+surveyNum+" and b.Stat_id="+surveyNum+"";
try{
stat=conn.createStatement();
rs=stat.executeQuery(sql);
String [] survey=new String[3];
int i=0;//
int j=0;//
while(rs.next())
{
survey[0]=rs.getString("Title");
survey[1]=rs.getString("Vote_class");
survey[2]=rs.getString("Vote_title");
if(survey[0]!=""&&j==0)
{
surveyInfo.append("<tr bgColor=#e6e6e6 align=left>");
surveyInfo.append("<td class=title1>" +"一周内最新评议题目:"+ survey[0] + " </td>");
surveyInfo.append("</tr>");
j++;
}
surveyInfo.append("<tr>");
surveyInfo.append("<td> <input type=radio width=50 name=survey value="+survey[1]+"/> ");
surveyInfo.append(survey[1]+" ");
surveyInfo.append(survey[2]+"</td>");
surveyInfo.append("</tr>");
i++;
}
if(i==0)
{
surveyInfo.append("<tr><td >没有内容!</td><tr>");
}
if(j==0)
{
surveyInfo.append("<tr><td >没有!</td><tr>");
}
}catch(Exception e){
System.out.println(e);
}
Dbconn a=new Dbconn();
a.dbClose(conn);
return surveyInfo;
}
fsurveyContent.jsp的代码如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="qtcommon.QuerySurveyBean" %>
<%!//处理字符串的方法
public String codeString(String s)
{
String str=s;
try{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}catch(Exception e)
{
return str;
}
}
%>
<html>
<head>
<title>
最新评议内容
</title>
<link rel="stylesheet" href="../css/style.css" type="text/css">
<script language="javascript">
<!--
function chkInput()
{
if(form1.survey.value=="")
{
alert("请选择您认为合适的评议项后再提交!");
return false;
}
return true;
}
//-->
</script>
</head>
<body bgcolor="#EFF3FF">
<form action="fsurveyContent.jsp" method="POST" name="form1">
<jsp:useBean id="surveyinfo" scope="application" class="qtcommon.QuerySurveyBean">
</jsp:useBean>
<% //获取最新评议的编码,在数据库中是bigint类型,在前一页面已经取出,正确
String surveynum1=request.getParameter("title");
java.lang.Long surveynum=new Long(Long.parseLong(surveynum1));
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#EFF3FF">
<jsp:setProperty name="surveyinfo" property="surveyNum" value="<%=surveynum%>"/>
<%
StringBuffer survey=surveyinfo.getSurveyInfo();
%>
<%=survey%>
<tr>
<td>
<input name="Button" type="submit" class="button" value="提交" onClick="return chkInput()">
<input class="button" type="reset" name="but1" value="重置" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<%
String voteinfo="";
//提交后,进行的操作
voteinfo=request.getParameter("survey");
voteinfo=codeString(voteinfo);
out.print("您投了"+voteinfo+"1票!");
%>
</body>
</html>
java.lang.NumberFormatException: null
提交的数据的数据类型不符 或者为空
上面的说的对
null也可能是你哪里对象没有实例化
大概看了下,我觉得这里应该有点问题吧
String surveynum1=request.getParameter("title");
java.lang.Long surveynum=new Long(Long.parseLong(surveynum1));
你第一次访问这个jsp页面的时候,也就是前一页面,应该有个title请求参数的吧,
所以进入jsp页面时String surveynum1=request.getParameter("title");取到的surveynum1为非空,java.lang.Long surveynum=new Long(Long.parseLong(surveynum1));转换不会出错,你的sql语句也能正常执行
然而你点击按钮的时候,再次进入这个jsp页面的时候,因为该页面没有title请求参数,所以String surveynum1=request.getParameter("title");取到的surveynum1为空,于是java.lang.Long surveynum=new Long(Long.parseLong(surveynum1));转换就出错了
从代码上看不出,建议楼主输出变量,判断是否没有初始化
java.lang.Long surveynum=new Long(Long.parseLong(surveynum1));
加上try,catch试试
提交的连接传递的值有问题吧,好好检查下值是怎么传的^^