0

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;
}
0

3 Answers 3

2

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\

Sign up to request clarification or add additional context in comments.

1 Comment

ojdbc14.jar is intended for Java 1.4. you should be using ojdbc5.jar(which is for Java 1.5) or ojdbc6.jar which is for Java 1.6.
1

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;
}

2 Comments

Eclipse makes it easy to ignore exceptions, as it creates a stacktrace on standard error by default. I immediately change that to a IllegalStateException("Exception not yet handled", e) and a TODO task. Just e.printStackTrace plays havoc with the internal state of methods as well (not being able to declare final variables outside the try, to name just one, or requiring a return after the catch). Throwing them helps as well of course.
@owlstead: agreed. I do the same thing. But to Eclipse's credit, at least it logs the exception and doesn't just completely swallows it.
1

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

2 Comments

I use this line to get connection into "connection" variable Connection connection = DatabazaOracle.connectDB(); here debugger say that variable connection is null
Just post the stacktrace of the exception here, with the relevant code. To obtain the stacktrace: exception.printStackTrace(), which will print it out in your console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.