0

I am trying to register new users to my database using jsp. When i do i get this error java.lang.NullPointerException

From this page i enter the variable to the database

<%@page import="storage.data"%>
<%
            String ps="";
            String v_name="";


             v_name =request.getParameter("vname");
             ps=request.getParameter("pass");

             data dt=new data();

               out.println(v_name + ps);

               try{

                    dt.st=dt.cn.createStatement();
                    out.println("1");
                    String insert="insert into voters values(myseq_voters.nextval,'"+v_name+"','"+ps+"',sysdate)";
                    out.println("2");
                    dt.st.executeUpdate(insert);
                    out.println("3");
                        //response.sendRedirect("adduser.jsp?usad="+1);
                }
                catch(Exception ex){
                out.println(ex);
                }
%>

And this is my code to connect it to the database

package storage;
import java.sql.*;


public class data {

        public Connection cn;
        public Statement st;
        public ResultSet rs;

        public data()
        {
            try{

                Class.forName("oracle.jdbc.OracleDriver");
                //cn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mustafadb","111");
                //jdbc:oracle:thin:@localhost:1521:XE [system on Default schema]
                cn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","voter_db","111");
            }
            catch(Exception ex){}
        }
}
MUSTAFA BURHANI
  • 53
  • 1
  • 1
  • 3
  • 1
    Do not use SQL directly in the jsp. Learn about prepared Statements to prevent SQL injection. **Never** write `catch(Exception ex){}`. Handle exceptions properbly atleast log the error – Jens Aug 18 '17 at 10:18
  • 1
    1. use debugger. 2 don't hide exception messages, maybe null is here?. 3. code has SQL injection. Agree with @Jens – Jacek Cz Aug 18 '17 at 10:20
  • @JacekCz My friend i have I have used this same code for another project and it seems to be working fine their. I dont know why its not working in this particular project. – MUSTAFA BURHANI Aug 18 '17 at 10:27
  • last but not least: pseudo "object" class, with wrong name ('data' have no meaning, and from small letter), wrong encapsulation, colntrol of data etc – Jacek Cz Aug 18 '17 at 10:30
  • @MUSTAFABURHANI Your friend is lucky. Code has many error, but not always occur – Jacek Cz Aug 18 '17 at 10:31
  • @JacekCz Okay my friend i finally found a solution. As i was comparing this project to one of my previous, I found that i did not add ojdbc file in the library which was causing this problem. now it work fine though . – MUSTAFA BURHANI Aug 18 '17 at 10:35
  • in my optinion: temporary hide one of problems, many other remains – Jacek Cz Aug 18 '17 at 10:36

0 Answers0