读书人

jsp中复选框取值有关问题,判空解决不了

发布时间: 2012-02-21 16:26:23 作者: rapoo

jsp中复选框取值问题,判空解决不了.(100分)
问题是,如果复选框选中了,这个页面不会出问题,但是如果没有选,就会报错,
我做了判断空值处理,if(testfx_ck1.length> 0),但是仍会报java.lang.NullPointerException错误,请问我错在哪里!


testfx.html
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
</head>

<body>

<form id= "form1 " name= "form1 " method= "post " action= "testfx.jsp ">
<p>
<input type= "checkbox " name= "mm " value= "a " onclick= "checkItem(this, 'mmAll ') " />
<br />
<input type= "checkbox " name= "mm " value= "b " onclick= "checkItem(this, 'mmAll ') " />
<br />
<input type= "checkbox " name= "mm " value= "c " onclick= "checkItem(this, 'mmAll ') " />
<br />
<input type= "checkbox " name= "mm " value= "d " onclick= "checkItem(this, 'mmAll ') " />
<br />
<input type= "checkbox " name= "mm " value= "e " onclick= "checkItem(this, 'mmAll ') " />
<br />
<input type= "checkbox " name= "mmAll " onclick= "checkAll(this, 'mm ') " />
全选与不全选的切换
<script language= "JavaScript " type= "text/javascript ">
</script>
</p>
<p>
<input type= "submit " name= "Submit " value= "提交 " />
</p>
</form>
<script language=javascript> function checkAll(e, itemName)
{
var aa = document.getElementsByName(itemName);
for (var i=0; i <aa.length; i++)
aa[i].checked = e.checked;
}
function checkItem(e, allName)
{
var all = document.getElementsByName(allName)[0];
if(!e.checked) all.checked = false;
else
{
var aa = document.getElementsByName(e.name);
for (var i=0; i <aa.length; i++)
if(!aa[i].checked) return;
all.checked = true;
}
}
</script>
</body>
</html>


testfx.jsp



<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
</head>

<body>
<%!
String testfx;
%>
<%testfx= " ";

String[] testfx_ck1=request.getParameterValues( "mm ");

if(testfx_ck1.length> 0){
for(int i=0;i <testfx_ck1.length;i++)
{
testfx=testfx+testfx_ck1[i]+ "- ";
}
}else{
%>
null
<%
}

%>
<%=testfx%>
</body>
</html>



[解决办法]
if(testfx_ck1.length> 0)--》
if(testfx_ck1==null&&request.getParameter( "mm ")!=null){
}


[解决办法]
if(testfx_ck1.length> 0)
---------------------

错在你假想testfx_ck1必然非空了。

去看看JavaDoc,request.getParameterValues()这个方法的详细描述。
如果request中该参数并不存在,那么返回值应该是null吧?

对于一个null,去取它并不存在的length属性,自然就得抛NullPointerException了。

读书人网 >Java Web开发

热点推荐