-1

I am doing simple jsp servlet program to check whether the logged user is valid or not?

If I am running db program seperately, then it is working fine that means connection and other things are correct.

But if I am integrating it with JSP & Servlet with valid inputs then it is throwing java.lang.NullPointerException.

Connection.java

public class ConnectionClass {
    public static Connection con=null;

    public static Connection getOracleConnection() {
        try {  
           Class.forName("oracle.jdbc.driver.OracleDriver");
           con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",Constants.OracleUsername,Constants.OraclePassword);  
        } catch(Exception e) {
           System.out.println("Error to create the connection ");
        }

        return con;
    }
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • Can you post your stacktrace? – Stephan Dec 07 '15 at 15:11
  • Once you're able to ask "Why is variable X null?" instead of "What is a NullPointerException?", feel free to ask a new question. You may then be able to get an answer like this: http://stackoverflow.com/questions/6792197/when-and-where-to-add-jar-files-to-war-project-without-facing-java-lang-classnot – BalusC Dec 07 '15 at 15:16

1 Answers1

0

When an exception occured, the method still return a connection. However, this connection may be null.

Try this insead:

public static Connection getOracleConnection() {
    try {  
       Class.forName("oracle.jdbc.driver.OracleDriver");
       con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",Constants.OracleUsername,Constants.OraclePassword);  
    } catch(Exception e) {
       System.out.println("Error to create the connection ");
       e.printStackTrace();
       throw new RuntimeException(e);
    }

    return con;
}
Stephan
  • 41,764
  • 65
  • 238
  • 329