当我将输出从Java类重定向到JSP页面时,我得到一个 java.lang.NullPointerException ,并且无法弄清楚出错了什么。任何帮助将不胜感激。
这是我的JSP应用程序的第一个表单页面:
<body>
<FORM METHOD=POST ACTION="formprocessing.jsp">
Enter 1st DATE: <INPUT TYPE=TEXT NAME=date1 SIZE=20><BR>
Enter 2nd DATE: <INPUT TYPE=TEXT NAME=date2 SIZE=20><BR>
Enter PARAM CODE: <INPUT TYPE=TEXT NAME=p_code SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</body>
此Java类将输入此处的输入值 set 放到输出页面上:
package duplicaterecords;
public class UserData {
String date1;
String date2;
String p_code;
public void setDate1( String value )
{
date1 = value;
}
public void setDate2( String value )
{
date2 = value;
}
public void setP_code( String value )
{
p_code = value;
}
public String getDate1() { return date1; }
public String getDate2() { return date2; }
public String getP_code() { return p_code; }
}
这是我的主要 Java类,我尝试将输出重定向到JSP页面:
package duplicaterecords;
import java.io.* ;
import java.sql.*;
import java.text.*;
import javax.servlet.http.*;
/*to find duplicate records and their time stamps*/
public class duplicaterecords extends HttpServlet {
public static void main(String[] args,HttpServletRequest request,
HttpServletResponse response)
{
int l,x=0,y=0,tow[],i=0,z=0,tower1=0,t=0;
String p_code,date[],date1,date2,getdate,date3,tower,t_split;
String time2;
tow=new int[1000];
date=new String[100];
DecimalFormat df = new DecimalFormat("#.##");
try
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); //input buffer
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt=con.createStatement();
String query1="select distinct(date) FROM `report_data` WHERE date>= ? and date<= ? "; //Query one for date input
PreparedStatement ps=con.prepareStatement(query1);
System.out.println("Enter the 1st DATE"); //Date 1 is entered
date1 = b.readLine();
System.out.println("Enter the 2nd DATE"); //Date 2 is entered
date2=b.readLine();
ps.setString(1,date1);
ps.setString(2,date2);
System.out.println("enter the param_code"); // param_code is entered
p_code= b.readLine();
ResultSet result=ps.executeQuery();
System.out.print("Tow_id");
while(result.next() )
{
getdate=result.getString("date");
System.out.print("\t"+getdate);
date3='%'+getdate+'%';
date[x]=date3;
x++;
}
l=x;
String query2="SELECT distinct(tow_id) FROM `tower_data` WHERE TIME_STAMP LIKE ? "; //query 2 for finding tower-id
PreparedStatement ps1=con.prepareStatement(query2);
ps1.setString(1,date[0]);
ResultSet result1=ps1.executeQuery();
while(result1.next())
{
System.out.println("");
tower=result1.getString("tow_id");
tower1= Integer.parseInt(tower);
tow[z]=tower1;
t=y;
System.out.print(tower1);
int count=0;
x=0;
request.getSession().setAttribute("tower_id", tow);
while(count<l)
{
String query3="SELECT time_stamp FROM tower_data WHERE `TIME_STAMP` LIKE ? AND `PARAM_CODE` = ? AND `TOW_ID`=? GROUP BY time_stamp HAVING count( * ) >1";
//Query 3 for finding time stamps with duplicate data
PreparedStatement ps2=con.prepareStatement(query3);
ps2.setString(2,p_code);
ps2.setString(1,date[x]);
ps2.setInt(3,tower1);
ResultSet result2=ps2.executeQuery();
int row=0;
while(result2.next())
{
t_split=result2.getString("time_stamp");
String[] parts= t_split.split(" "); //splitting time_stamp to extract only time without date
time2=parts[1]; //time stored in time2
System.out.println("\t"+time2);
row++;
}
if(row==0)
{
System.out.println("\t"+"no duplicate");
}
System.out.print("\t"+"\t");
x++;
count++;
}
}
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
最后这是输出JSP页面,它必须显示Java类的输出:
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<html>
<body>
You entered<BR>
Date1: <%= duplicaterecords.getDate1() %><BR>
Date2: <%= duplicaterecords.getDate2() %><BR>
PARAM CODE: <%= duplicaterecords.getP_code() %><BR>
<table border="1">
<tr><td><B>Tower ID:</B></td></tr>
<%
ArrayList towerArray = (ArrayList) session.getAttribute("tower_id");
String myString="";
for(int i = 0; i < towerArray.size(); i++)
{
myString = (String) towerArray.get(i);
}
%>
<tr><td><%=myString%></td></tr>
</table>
</body>
</html>
任何帮助?
答案 0 :(得分:0)
我没有看到实际的错误,所以这只是猜测,但你正在做:
request.getSession().setAttribute("tower_id", tow);
其中tow是一个int数组。
然后在JSP中,你说:
ArrayList towerArray = (ArrayList) session.getAttribute("tower_id");
所以这些类型不兼容。
此外,与您的问题无关,我会建议您,代码质量一般似乎非常糟糕。我建议阅读更多关于Java约定等的内容。
答案 1 :(得分:0)
行动中 request.getSession()。setAttribute(“tower_id”,tow);
在jsp中
ArrayList towerArray = (ArrayList) session.getAttribute("tower_id");
但是tow是一个数组
int l,x=0,y=0,tow[],i=0,z=0,tower1=0,t=0;
但除此之外,为什么在servlet代码中有一个静态的main方法,doGet或doPost在哪里?
这就是为什么你看到NPE而不是类强制转换异常的原因。您的会话中没有tower_id属性,因此当您执行此操作时
towerArray.size()
你得到了NPE