锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / java开源 / struts连接数据库出现java.lang.NullPointerException 异常
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

java.lang.NullPointerException during database conectivity in struts


Hi, I am making database connectivity in struts and getting error : javax.servlet.ServletException: java.lang.NullPointerException, code: 嗨,我在struts制作数据库连接和获取错误:javax.servlet.ServletException:显示java.lang.NullPointerException,代码:

config.xml:

<form-beans>
<form-bean name="DemoTestForm" type="DemoTestForm"/>
</form-beans>
<action-mappings>
<action input="/index.jsp" name="DemoTestForm" parameter="testMethod" path="/test" scope="request" type="DemoTestAction">
<forward name="success" path="/welcomeStruts.jsp"/>
<forward name="fail" path="/Failure.jsp"/>
</action>
</action-mappings>

DBConnect.java:

import java.sql.*;
public class DBConnect
{
Connection con=null;
String result;
public String checkUser(String name,String pass)
throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection
("jdbc:oracle:thin@localhost:1521:xe", "system", "java");
}catch(ClassNotFoundException cnf)
{
cnf.getMessage();
}
finally
{
con.close();
}
return(result);
}
}

DemoAction.java:

public class DemoTestAction
extends org.apache.struts.action.Action {
@Override
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
DemoTestForm f = (DemoTestForm)form;
String name = f.getTextName();
String pass = f.getTextPass();
String logic=null;
DBConnect db=new DBConnect();
db.checkUser(name, pass);
Statement stmt=null;
ResultSet rs=null;
stmt=db.con.createStatement();
rs=stmt.executeQuery("select * from admin");
while (rs.next())
{
pass.equals(rs.getString(3))))
if (name.equals(rs.getString(2)) && pass.equals(rs.getString(3))) {
logic="success";
break;
}
else
{
logic="fail";
}
}
return mapping.findForward(logic);
}
}

index.jsp:

<html:form action="/test">
Name: <html:text property="textName"/><br>
Password:   <html:password property="textPass"/><br>
<html:submit property="loginButton" value="Login Here"/>
</html:form
>

Please help solve my problem. 请大家帮忙解决我的问题。

added some formatting; needs more. 增加了一些格式;需要更多。

Try debugging it. Somewhere in this, you are accessing an object that has not been initilised, hence null pointer. 尝试调试它。某处在此,您访问尚未initilised的对象,因此,空指针。

i have already debug my program and i have initialized all object .....so plzz check my prog ... 我已经调试我的程序,我已经初始化所有的对象.....所以plzz检查我PROG ...

There is alot of information missing: 有资料丢失了很多:

- Stack trace? - 堆栈跟踪?

- If you have debugged, what line throws the exception? - 如果你已经调试,哪一行抛出异常?

Please add these to the question - click the green 'Improve question' text and maybe I, or someone, can see where the problem lies. 请把这些加入到这个问题 - 点击绿色的“改善问题”的文字,也许我还是有人,可以看到问题所在。

Finally, try spelling words in full and using capitalisation. There is nothing to get people more annoyed on cp then 'i', 'plzz' or 'gizus codz'. 最后,尝试拼写单词全部使用大写字母。没有什么可以让人们对CP更恼火然后'我','plzz'或'gizus codz“。

And I've formatted your code for you. 我已经格式化你的代码你。

Look at the following code: 请看下面的代码:

try {      
Class.forName("oracle.jdbc.driver.OracleDriver");      
con = DriverManager.getConnection              
("jdbc:oracle:thin@localhost:1521:xe", "system", "java");  
}  
catch (ClassNotFoundException cnf) {      
cnf.getMessage();  
}  
finally  
{      
con.close();  
}  
return(result);

Every time the code is executed the finally block is executed. It is there to clean up after a try block and is executed irrespective of if there has been an exception or not. 每次执行代码时的最终块被执行。它是有一个try块后清理,并不管是否出现了异常或不执行。

So after opening a DB connection you close it. 所以打开后关闭它一个数据库连接。

Also, even though it's not used, result is not being set. 另外,即使它没有被使用,结果是不被设置。

友情链接
版权所有 Copyright(c)2004-2024 锐英源软件
统一社会信用代码:91410105098562502G 豫ICP备08007559号 最佳分辨率 1440*900
地址:郑州市金水区文化路97号郑州大学北区院内南门附近