当前位置: 技术问答>java相关
初学者:一个函数参数的问题,请大家帮忙!
来源: 互联网 发布时间:2015-11-17
本文导语: 我在JSP中调用了下面的函数,当传入参数guessNum的值为null时,程序就会出错,不知为何,请大家指教。 public int[] checkNumber(String guessNum,String answerNum) { if(guessNum.length()!=4) return new int[] {-3,0,0}; ...
我在JSP中调用了下面的函数,当传入参数guessNum的值为null时,程序就会出错,不知为何,请大家指教。
public int[] checkNumber(String guessNum,String answerNum)
{
if(guessNum.length()!=4) return new int[] {-3,0,0};
int num=0;
try{
num=Integer.parseInt(guessNum);
}
catch(NumberFormatException ex){
return new int[] {-2,0,0};
}
int[] timesDigit={0,0,0,0,0,0,0,0,0,0};
for (int i=0;i1)
return new int[] {-1,0,0};
}
int a=0;
int b=0;
for (int j=0;j=0) b++;
}
if (a==4) return new int[] {0,a,b};
else
return new int[] {1,a,b};
}
出错信息如下:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
........
root cause
java.lang.NullPointerException
at org.apache.jsp.debug_jsp.checkNumber(debug_jsp.java:25)
at org.apache.jsp.debug_jsp._jspService(debug_jsp.java:112)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:204)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
.........
public int[] checkNumber(String guessNum,String answerNum)
{
if(guessNum.length()!=4) return new int[] {-3,0,0};
int num=0;
try{
num=Integer.parseInt(guessNum);
}
catch(NumberFormatException ex){
return new int[] {-2,0,0};
}
int[] timesDigit={0,0,0,0,0,0,0,0,0,0};
for (int i=0;i1)
return new int[] {-1,0,0};
}
int a=0;
int b=0;
for (int j=0;j=0) b++;
}
if (a==4) return new int[] {0,a,b};
else
return new int[] {1,a,b};
}
出错信息如下:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
........
root cause
java.lang.NullPointerException
at org.apache.jsp.debug_jsp.checkNumber(debug_jsp.java:25)
at org.apache.jsp.debug_jsp._jspService(debug_jsp.java:112)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:204)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
.........
|
String guessNum
在判断语句中中加上
if(guessNum==null)
当guessNum为null的时间是要抛出NullPointerException的
在判断语句中中加上
if(guessNum==null)
当guessNum为null的时间是要抛出NullPointerException的