0

Im very beginner in Java servlets... i have tried to connect servlet with database but NullPointerException

I tried to resolve this problem ,but no luck...

Error is

java.lang.NullPointerException JDBCServlet.service(JDBCServlet.java:(42)

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.DriverManager;//1
import java.sql.Connection;//2
import java.sql.SQLException;
import java.sql.Statement;//3
import java.sql.ResultSet;//4

public class JDBCServlet extends HttpServlet {
    Connection con;
     Statement st;
    ResultSet rs;
    String sql;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("JDBC Servlet Invoked");

        //1st step: load JDBC MySql Drivers
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Drivers Loaded");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //2nd Step: create a connection
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webtech1", "root", "123456");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String sql = "select * from user";
        try {
            st = con.createStatement();
            rs = st.executeQuery(sql);

            while(rs.next()){

                System.out.println(rs.getInt("id"));
                System.out.println(rs.getString("firstName"));
                System.out.println(rs.getString("lastName"));
                System.out.println(rs.getString("email"));
                System.out.println(rs.getString("password"));
                System.out.println(rs.getString("createdate"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Asad
  • 57
  • 1
  • 2
  • 10
  • 1
    I guess the line no 42 is `st = con.createStatement();`, Did you check STDOUT for any DB Connection error? If yes what is the error? – Saravana Oct 12 '15 at 06:32
  • I guess your connection object is null. CheckList -> 1) Is your driver loaded? 2) Can you debug and find if you are able to get connection object as not null? – a3.14_Infinity Oct 12 '15 at 06:40
  • yes error in this line..st = con.createStatement(); but don't know why... db connection is working... i have tested it .... – Asad Oct 12 '15 at 06:41
  • 1 driver loaded fine.. error in just st = con.createStatement(); in this line.. before this line all code worked... – Asad Oct 12 '15 at 06:45

2 Answers2

0

Please put null checks on your con,st,rs objects. And also double check your mysql connection string.

bsingh
  • 145
  • 1
  • 6
0

You have initialised String sql twice. One as a property and one as a local variable i.e.

String sql = "select * from user";

Later, you are calling sql (an instance variable) which is indeed of null value, which causes null pointer exception. You just need to remove String declaration from the body of your service method.

Gitnik
  • 564
  • 7
  • 26
  • After removing String from Services method Area still same error i have find.. – Asad Oct 12 '15 at 06:56
  • @Asad Sorry, I'm at loss then. Maybe you have not added appropriate MySql `.jar` file to the build path. – Gitnik Oct 12 '15 at 09:03