1 Reply Latest reply on Sep 16, 2005 7:25 AM by Njan van Kalip

    java.lang.NullPointerException

    Njan van Kalip Newbie

      This problem has been bothering me for quite a long time now. I created a class to handle all databse transactions. I am using postgresql on jboss 4.0.2. In fact this class is used in a web service. But whenever a query is executed (or updated) I get a NullPointerException. I cannot figure out why. I tried something similar in a java standalone program and it was workinig fine. I was wondering if this was an issue particular to JBoss.

      Here is my code:-


      import java.sql.*;
      
      public class Transaction {
      
      private Connection conn;
      private Statement s;
      private ResultSet rs;
      
      
      public boolean commit() throws SQLException{
       conn.commit();
       return true;
      }
      
      public boolean rollback() throws SQLException{
       conn.rollback();
       return true;
      }
      
      public boolean close() throws SQLException{
       if (conn!=null) conn.close();
       return true;
      
      
      }
      
      
      public Transaction(){
      
       try{
       Class.forName("org.postgresql.Driver");
       log.info("Driver found");
      
       }
       catch(ClassNotFoundException e){}
      
       try {
       conn=DriverManager.getConnection("jdbc:postgresql://localhost/dbname","username","password");
      
       }
       catch(SQLException e){e.printStackTrace();}
       finally {
       try{
       if (conn!=null) conn.close();
       }catch(SQLException e){}
       }
      
      
      }
      
      
      public int update(String query) throws SQLException{
      
       s=conn.createStatement();
       int t=s.executeUpdate(query);
       s.close();
       return t;
      }
      
      public ResultSet select(String query) throws SQLException{
      
      rs=s.executeQuery(query);
      return rs;
      
      }
      
      }
      
      
      


      The following is part of the stack trace:-

      22:49:54,844 INFO [STDOUT] java.lang.NullPointerException
      22:49:54,845 INFO [STDOUT] at data.Transaction.update(Unknown Source)
      22:49:54,845 INFO [STDOUT] at CustomerBooking.addCustomer(CustomerBooking.java:30)
      22:49:54,846 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      22:49:54,846 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      22:49:54,846 INFO [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)