title: java Prepared Statement fails on null error
body: <p>I've got a problem I can't figure out, i'm sure it's easy but my google searches and Stackoverflow searches didn't get me there. The java documentation wasn't helping neither.
  I've found a lot of problems where people make syntaxing errors in the SQL statement or in the code. I think mine is in the code.
  It's about getting some information from the database by a query where the user can fill in it's own search information (a number).</p>
  
  <p>Here is the code:
  (I've tried multiple things so it might be that my code isn't the best anymore. Also I am new to java so keep that in consideration.)</p>
  
  <pre><code>    /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package dbConnection;
  
  import data.Mail;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.util.ArrayList;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import view.SelectionScreenCRA;
  
  /**
   *
   * @author vm
   */
  public class dbConnect {
  
      public static Connection getConnection() {
          String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
          String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Users\\Jeroen Veldhuijzen\\locaties.mdb;";
          String username = "";
          String password = "";
  
          try {
              Class.forName(driver);
              return DriverManager.getConnection(url, "", "");
  
          } catch (Exception ex) {
              Logger.getLogger(dbConnect.class.getName()).log(Level.SEVERE, null, ex);
              System.out.println("\nError: " + ex.toString() + ex.getMessage());
  
          }
          return null;
      }
  
      public static ArrayList&lt;dbConnect&gt; executeQuery(int cra) {
  
  
           ArrayList&lt;dbConnect&gt; locatie = new ArrayList&lt;dbConnect&gt;();
  
          Mail mail = new Mail();
          try{
          Connection conn = getConnection();
          PreparedStatement pst = conn.prepareStatement(
                  "SELECT Locatiegegevens.[Locatiegegevens], "
                  + "Locatiegegevens.[ISP - Straatnaam], "
                  + "Locatiegegevens.[ISP - Huisnummer],"
                  + " Locatiegegevens.[ISP - Postcode], "
                  + "Locatiegegevens.[ISP - Plaatsnaam], "
                  + "Locatiegegevens.[G4S - Transportcode] "
                  + "FROM Locatiegegevens "
                  + "WHERE Locatiegegevens.[Locatiegegevens] = '?';");
  
  
          pst.setInt(1, cra);
          pst.execute();
  
          pst.close();
          conn.close();
  
          }catch (Exception e){
              System.out.println("ERRROOOOR");
  
              System.out.println(e.getMessage());
              System.out.println(e.getClass());
              Logger.getLogger(dbConnect.class.getName()).log(Level.SEVERE, null, e);
  
          }
          return locatie;
  
      }
  
  }
  </code></pre>
  
  <p>The int cra will be set here:</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      dbConnect dbc = new dbConnect();
      dbConnect.executeQuery(Integer.parseInt(getCRA().getText()));        
  }
  </code></pre>
  
  <p>The cra will be retrieved as a String and then parsed into an int when it will be passed to the executeQuery command.
  If I sout the cra int, it will give me the int 9501 which is filled in in the textbox.
  But if I run the query by pushing the button jButton1, there will be a null error:</p>
  
  <pre><code>null
  
  class java.lang.NullPointerException
  aug 09, 2012 12:43:04 PM dbConnection.dbConnect executeQuery
  SEVERE: null
  java.lang.NullPointerException
      at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(JdbcOdbcPreparedStatement.java:1022)
      at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setInt(JdbcOdbcPreparedStatement.java:526)
      at dbConnection.dbConnect.executeQuery(dbConnect.java:62)
      at view.SelectionScreenCRA.jButton1ActionPerformed(SelectionScreenCRA.java:502)
      at view.SelectionScreenCRA.access$900(SelectionScreenCRA.java:29)
      at view.SelectionScreenCRA$11.actionPerformed(SelectionScreenCRA.java:333)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.Component.processMouseEvent(Component.java:6504)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
      at java.awt.Component.processEvent(Component.java:6269)
      at java.awt.Container.processEvent(Container.java:2229)
      at java.awt.Component.dispatchEventImpl(Component.java:4860)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4686)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
      at java.awt.Container.dispatchEventImpl(Container.java:2273)
      at java.awt.Window.dispatchEventImpl(Window.java:2713)
      at java.awt.Component.dispatchEvent(Component.java:4686)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
      at java.awt.EventQueue.access$000(EventQueue.java:101)
      at java.awt.EventQueue$3.run(EventQueue.java:666)
      at java.awt.EventQueue$3.run(EventQueue.java:664)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:680)
      at java.awt.EventQueue$4.run(EventQueue.java:678)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  </code></pre>
  
  <p>Is there someone who could point me in the right direction?</p>