Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am making database connectivity in struts and getting error:javax.servlet.ServletException: java.lang.NullPointerException, code:-
config.xml:
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:
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:
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:
XML
<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.
Posted
Updated 11-Nov-10 21:44pm
v4
Comments
Nagy Vilmos 11-Nov-10 16:59pm    
added some formatting; needs more.
Nagy Vilmos 11-Nov-10 17:01pm    
@amitpinku Try debugging it. Somewhere in this, you are accessing an object that has not been initilised, hence null pointer.
amitpinku8309 12-Nov-10 2:26am    
i have already debug my program and i have initialized all object .....so plzz check my prog ...
Nagy Vilmos 12-Nov-10 3:27am    
@amitpinku 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'.
Nagy Vilmos 12-Nov-10 3:44am    
And I've formatted your code for you.

1 solution

Look at the following code:
C#
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.

So after opening a DB connection you close it.

Also, even though it's not used, result is not being set.
 
Share this answer
 
v2
Comments
Dalek Dave 12-Nov-10 4:23am    
Good Call young man!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900