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()不起作用,爲什麼它出錯。很感謝任何形式的幫助!!JSP java.lang.NullPointerException
<%
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");
}
你能粘貼確切的錯誤信息(帶行號......)嗎? – Frithjof
看起來您每次都在循環中調用sort,所以第一次2個元素將爲null,因此爲NullPointerException。 – Samhain
什麼是以下'java.util.Arrays.sort(myRows)的行號碼; '? –