Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread “main” java.lang.NullPointerException error JDeveloper 12c

1058780Dec 9 2013 — edited Dec 10 2013

Hello,

I am trying to call a java stored procedure in java application. I am using ORACLE database and JDeveloper.

I am getting error "Exception in thread "main" java.lang.NullPointerException. I have no idea what have I been doing wrong.

I have a table "Beer" and I want to select all the data out with a stored procedure which I call out of Java app.

I have a java.class file Store_A.java which I have loaded into the ORACLE database with LOADJAVA :

    import java.sql.*;

    import java.io.*;

 

    public class Store_a {

 

      public static void apskatit ()

        throws SQLException

        { String sql =

          "SELECT * FROM Beer";

        try { Connection conn = DriverManager.getConnection("jdbc:default:connection:");

          PreparedStatement pstmt = conn.prepareStatement(sql);

          ResultSet rset = pstmt.executeQuery();

          rset.close();

          pstmt.close();

          }

        catch (SQLException e) {System.err.println(e.getMessage());

        }

      }

    }

Then I have created a procedure which I plan to call out in java:

    CREATE OR REPLACE PACKAGE Store_a AS

    PROCEDURE apskatit;

    END Store_a;

 

    CREATE OR REPLACE PACKAGE BODY Store_a AS

    PROCEDURE apskatit AS LANGUAGE JAVA

    NAME 'Store_a.apskatit()';

    END Store_a;

And I have a java file that I have created with JDeveloper 12c:

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.SQLException;

 

    public class Class1 {

        /**

         * @param args

         */

        public static void main(String[] args) throws SQLException {

            Connection conn = null;

            try {

                Class.forName("oracle.jdbc.driver.OracleDriver");

                //Izveidojam savienojumu

                conn = DriverManager.getConnection("jdbc.oracle.thin:@localhost:1521", "SYSTEM", "asdasd");

                // Izveidojam callable statement

                CallableStatement stmt = conn.prepareCall("CALL Store_a.apskatit()");

                ResultSet resul = stmt.executeQuery();

                while (resul.next()) {

                    System.out.println(resul.getInt(1) + "\t" + resul.getString(2));

                }

            } catch (ClassNotFoundException e) {

                e.printStackTrace();

            } finally {

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

        }

    }

When I try to run the java file, I get this error "Exception in thread "main" java.lang.NullPointerException at client.Class1.main(Class1.java:29).

So the line I get error in is "conn.close();"

How to fix this?

Thank you very much in advance.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2014
Added on Dec 9 2013
2 comments
2,474 views