JSP java.lang.NullPointerException

时间:2013-10-15 15:16:01

标签: java jsp

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
java.util.Arrays.mergeSort(Arrays.java:1156)
java.util.Arrays.sort(Arrays.java:1080)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我正在尝试对字符串数组进行排序并接收java.lang.NullPointerException。这只是代码的一部分,但它是与错误相关的相关部分。我知道当我初始化字符串数组myRows时,它将值设置为null。但是,我正在检查空值并将item_value,period_value和periodAmt_value的值设置为设置值。当我打印出数组的内容时,它确实包含设置值。不明白为什么java.util.Arrays.sort()不起作用以及为什么它出错。非常感谢任何帮助!!

<%

String item_name;
String item_value;

String period_name;
String period_value;

String periodAmt_name;
String periodAmt_value;

String annualAmt_name;
int error_count = 0;
int annualAmt_value = 0;
int totalExp = 0;

String[] myRows = new String[3];


for (int i=0; i < 3; i++)
{
item_name = "item" + i;
item_value = request.getParameter(item_name);

if (item_value == null) 
{
        item_value = "Rent";
}

period_name = "period" + i;
period_value = request.getParameter(period_name);

if (period_value == null) 
{
    period_value = "52";  //default value
}

periodAmt_name = "periodAmt" + i;
periodAmt_value = request.getParameter(periodAmt_name);

if (periodAmt_value == null) 
{
    periodAmt_value = "520";
}


myRows[i] = item_value + "|" + period_value + "|" + periodAmt_value;

out.println(myRows[i]);


java.util.Arrays.sort(myRows);


String text = myRows[i];
StringTokenizer st = new StringTokenizer(text, "|");
String[] myFields = new String[st.countTokens()];

out.println("<tr>");

for (int j = 0; j < myFields.length; j++)
{ 
    myFields[j] = st.nextToken();


    if(j==0)
    {
    out.println("<td><input type=text name=" + item_name + " value=" + myFields[j] + "></td>\n");
    }

1 个答案:

答案 0 :(得分:0)

查看异常日志后,我可以高度肯定地说您的问题是由于尝试对包含null元素的数组进行排序。鉴于您的代码流程,我强烈反对我之前建议完整地构建您的数组,然后对其进行排序,然后使用它来生成您的html。