title: Java NullPointerException on createStatement
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7132614/what-can-i-do-to-avoid-a-nullpointerexception">What can I do to avoid a NullPointerException?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm developing an application for a restaurant. The error occurs when I try to initialize a certain class. Here is the code.</p>
  
  <pre><code>public class DbItem {
  
  private static Connection conn = null;
  
  static {
      conn = DBConnection.connect();
  }
  public static ArrayList&lt;Item&gt; fetchAll() {
      ArrayList&lt;Item&gt; items = new ArrayList&lt;&gt;();
      String sql = "select * from itens";
      try {
          Statement st = conn.createStatement();
          ResultSet rs = st.executeQuery(sql);
          while(rs.next()) {
              items.add(new Item(rs.getString("nome"), rs.getString("descricao"), rs.getFloat("preco")));
          }
          return items;
      } catch (SQLException ex) {
          Logger.getLogger(DbItem.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
  }
  }
  </code></pre>
  
  <p>The connection is taken from this class</p>
  
  <pre><code>public class DBConnection {
  
  private static Connection conn;
  
  public static Connection connect() {
      if(conn == null) {
          String url = "jdbc:mysql://localhost:3306/";
          String dbName = "restaurante_comandas_jonnathan";
          String driver = "com.mysql.jdbc.Driver";
          String userName = "root";
          String password = "root";
          try {
              Class.forName(driver).newInstance();
              conn = DriverManager.getConnection(url + dbName, userName, password);
              System.out.println("Connected to " + dbName + " with " + userName);
              JOptionPane.showMessageDialog(null, conn == null);
              return conn;
          } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
              e.printStackTrace();
          }
      }
      return null;
  }
  
  }
  </code></pre>
  
  <p>I tried debugging using popup boxes. I checked if the connection was null right before the return command, and found out is was not. Then, I checked after the call on the first script, and found out it was, indeed, null. Now, how can an object be null right after the retrieving if it wasn't before the return?</p>
  
  <p>Edit:</p>
  
  <p>Here it is. The stack trace</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at model.data.DbItem.fetchAll(DbItem.java:48)
  at model.Item.getAll(Item.java:76)
  at view.PedidoForm.&lt;init&gt;(PedidoForm.java:22)
  at view.Login$2.run(Login.java:99)
  at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
  at java.awt.EventQueue.access$200(EventQueue.java:103)
  at java.awt.EventQueue$3.run(EventQueue.java:682)
  at java.awt.EventQueue$3.run(EventQueue.java:680)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  </code></pre>