Peter
Peter

Reputation: 31

connecting to oracle DB in Java returns NullPointerException

I have this method to connecting to oracle 11g xe. It´s still returning exception java.lang.NullPointerException. I use Eclipse IDE. Please help, i don´t know how to fix it.

public static Connection connectDB() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {

    Connection connection = null;
    try {
        // Load the JDBC driver
        String driverName = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driverName).newInstance();

        // Create a connection to the database
        String serverName = "127.0.0.1";
        String portNumber = "1521";
        String sid = "xe";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "peter";
        String password = "pass";
        connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
    } catch (ClassNotFoundException e) {
        // Could not find the database driver
    } catch (SQLException e) {
        // Could not connect to the database
    }
    System.out.println(connection);
    return connection;
}

Upvotes: 0

Views: 7988

Answers (3)

Peter
Peter

Reputation: 31

Problem was in driver for oracle. I solve that by putting a i need put ojdbc14.jar file into server folder. I´m using tomcat, so i put ojdbc14.jar file into folder apache-tomcat-7.0.8\lib\

Upvotes: 2

Robin
Robin

Reputation: 36601

I think even when the stacktrace is posted, the NullPointerException does not occur in the piece of code posted in the question. There are only two lines which are not variable initializations or simple System.out.println statements (which can handle null anyway).

There is the

Class.forName( driverName ).newInstance();

line which can cause an exception when Class.forName( driverName ) is not found. However, this would cause a ClassNotFoundException and not a NullPointerException occurs, so this can be eliminated.

The only other line which might cause an exception is the

connection = DriverManager.getConnection(url, username, password);

line. However, url, username and password are all non-null so this line wouldn't cause the exception either.

Bottom line, obtain the stacktrace, analyze it yourself (trivial when using an IDE, just click in the stacktrace and see where your IDE takes you) and if needed post it here with the relevant piece of code.

Oh, and why do you declare your method to throw the ClassNotFoundException if you catch it and ignore it in the implementation of the method. This must be like the worst of both worlds

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691645

You ignore exceptions, and then hope that everything went well. Just stop ignoring exceptions, and you'll probably have the root cause of your NPE:

public static Connection connectDB() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {

    Connection connection = null;
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName).newInstance();

    // Create a connection to the database
    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "xe";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "peter";
    String password = "pass";
    connection = DriverManager.getConnection(url, username, password);
    System.out.println(connection);

    System.out.println(connection);
    return connection;
}

Upvotes: 1

Related Questions