Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java struts
Hi, I am making database connectivity in struts and getting error:javax.servlet.ServletException: java.lang.NullPointerException, code:-
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.
Posted 11-Nov-10 8:24am
Edited 11-Nov-10 22:44pm
Nagy Vilmos275.1K
v4
Comments
Nagy Vilmos at 11-Nov-10 16:59pm
   
added some formatting; needs more.
Nagy Vilmos at 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 at 12-Nov-10 2:26am
   
i have already debug my program and i have initialized all object .....so plzz check my prog ...
Nagy Vilmos at 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 at 12-Nov-10 3:44am
   
And I've formatted your code for you.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
So after opening a DB connection you close it.
 
Also, even though it's not used, result is not being set.
  Permalink  
v2
Comments
Dalek Dave at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100