我的 Java/JSP 应用程序中的 java.lang.NullPointerException
当我将 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 类将此处的输入值设置到输出页面:
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; }
}
这是我的主要 我尝试将输出重定向到 JSP 页面的 Java 类:
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();
}
}
}
最后,这是必须显示 Java 类的输出的输出 JSP 页面:
<%@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>
有帮助吗?
I am getting a java.lang.NullPointerException when I am redirecting the output from a Java class to a JSP page and cannot figure out what's wrong. Any help would be greatly appreciated.
Here is my the first form page of my JSP application:
<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>
The input values from here are then set onto the output page by this Java class:
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; }
}
This is my main Java class from which I am trying to redirect the output to the JSP page:
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();
}
}
}
And finally this is the output JSP page which has to show the output from the Java class:
<%@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>
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有看到实际的错误,所以这只是一个猜测,但你正在做:
request.getSession().setAttribute("tower_id", tow);
其中 tow 是一个 int 数组。
然后在 JSP 中,您会说:
ArrayList towerArray = (ArrayList) session.getAttribute("tower_id");
所以这些类型不兼容。
另外,与您的问题无关,我建议您,代码质量总体上似乎相当糟糕。我建议阅读更多有关 Java 约定等的内容。
I haven't seen the actual error so it is just a guess but you are doing:
request.getSession().setAttribute("tower_id", tow);
where tow is an int array.
Then in the JSP, you are saying:
ArrayList towerArray = (ArrayList) session.getAttribute("tower_id");
So those types aren't compatible.
Also, unrelated to your question I would advise you that the code quality in general seems to be quite bad. I would suggest reading more about Java conventions, etc.
行动中
request.getSession().setAttribute("tower_id", tow);
在jsp中,
但tow是一个数组
,但除此之外,为什么在servlet代码中有一个静态的main方法,doGet或doPost在哪里?
这就是为什么您看到的是 NPE 而不是类转换异常。您的会话中没有 tower_id 属性,因此当您执行此操作时,
您会得到 NPE
in action
request.getSession().setAttribute("tower_id", tow);
in jsp
but tow is an array
but beyond all that, why is there a main method that's static in your servlet code, and where is the doGet or doPost?
Which is why you are seeing the NPE and not a class cast exception. There is no tower_id attribute in your session, and so when you do this
you get the NPE