1
votes
public class MyBean {

    private Connection con;

    Set combinations;
    public MyBean()
    {

        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
            con.setAutoCommit(true);

        }
        catch( ClassNotFoundException cfe )
        {
            cfe.printStackTrace();
        }
        catch( SQLException se )
        {
            se.printStackTrace();
        }

    }

      public boolean CustRegistration( String name,String username, String pwd, String  address, String city,String state,long phoneno)
     {
         try
          {
       PreparedStatement st = con.prepareStatement("insert into custreg values(?,?,?,?,?,?,?)");
                 st.setString(1,name);
             st.setString(2,username);
             st.setString(3,password );
             st.setString( 4,address);
             st.setString(5,city);
             st.setString(6,state);
             st.setLong(7 ,phoneno);
             int i = st.executeUpdate();

             if( i > 0 )
                 return true;

         }
         catch( SQLException sety )
         {
             sety.printStackTrace();
         }
         return false;       
     }
public boolean check_Customer( String username,String password )
{
    try
    {
    PreparedStatement st;   
    st = con.prepareStatement( "select * from custreg where username=? and password=?" );

        st.setString(1,username);

        st.setString(2,password );

        ResultSet rs = st.executeQuery();

        if( rs.next() )
        {
            st.close(); 
            return true;        
        }

        st.close();
    }
    catch( SQLException sert )
    {
        sert.printStackTrace();
    }

    return false;
}

//////////////////////////////////////////////
when ever i try to connect with database it throws NULL POINTER EXCEPTION The stack trace is:

java.lang.NullPointerException
    Bean.MyBean.check_Customer(MyBean.java:35)// line 35 is prepared statement which uses connection             
 Servlets.LoginSubmit.doPost(LoginSubmit.java:63)//the code here is 
if( mybean.check_Customer( user,pass ) )

javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

2
prepareStatement can throw SQLException (not NPE). Did you check that con isn't null?Maroun
where is your check_Customer method?Anubhab
@beigh look at the code you posted on the comments. Do you think it is readable?Maroun
@MarounMaroun i have updated my questionbeigh
@beigh I suggest you to replace the query "select * from custreg where username=? and password=?" with something else that you're sure will work, maybe the problem with your query.Maroun

2 Answers

0
votes

I'm assuming check_Customer in the stacktrace is actually the CustRegistration method. In that case it means that the con field is not set (it is null).

In the constructor you are catching exceptions and then just printing them. I think you got one of these exceptions. Maybe you don't have the oracle driver in your classpath?

0
votes

Most probably your DB is DOWN so con is null because it cannot get a connection.