title: object constructed but still null
body: <p>What is wrong with this source code?</p>
  
  <pre><code> 1  package Core;
   2  
   3  import java.sql.*;
   4  
   5  public class Course extends Model {
   6      
   7      protected int id;
   8      protected String title;
   9      
  10      public Course(int id) {
  11          this.id = id;
  12      }
  13      
  14       public static Course getNew(ResultSet res) {
  15          try {
  16              Course c;
  17              c = new Course(res.getInt("course_id"));
  18              return c;
  19          } catch(SQLException e) {
  20              return null;
  21          }
  22      }
  23      
  24      @Override
  25      public String toString() {
  26          return this.title;
  27      }
  28  }
  </code></pre>
  
  <p>I've set breakpoints on lines 17, 18 and 11 (mentioned in the order of the execution flow), the object is certainly constructed, yet on line 18 it will return a <code>null</code> instead of the object.</p>
  
  <p>What am I missing?</p>
  
  <p>Please don't tell me how bad static methods are, I know that. Help me understand why is this happenning and how to fix it.</p>
  
  <p>I'm using netbeans + glassfish + java 7 (openjdk) on linux x64.</p>
  
  <p><strong>Addendum</strong></p>
  
  <p>Ok so the problem may be in the way I'm calling it - I'm using reflection and generics.</p>
  
  <p>The caller looks like this</p>
  
  <pre><code> 1  package Core;
   2  
   3  import java.sql.*;
   4  import java.util.*;
   5  import java.lang.reflect.*;
   6  
   7  /**
   8   * Represents storable elements in a database
   9   */
  10  public abstract class Model {
  11      /**
  12       * this attribute is common to all models of the app
  13       * this way we can reuse the same connection for
  14       * everything (good or bad, depending on the project's requirements; in
  15       * our case it's good - this should be a simple application, no mysql
  16       * replication and things like that)
  17       */
  18      protected static Connection conn = null;
  19      
  20       public static void setConnection(Connection c) {
  21          Model.conn = c;
  22      }
  23       
  24       public static void initStatements() throws SQLException {
  25           
  26       }
  27       
  28       protected &lt;T extends Model&gt; HashMap&lt;String, Model&gt; resultsetMap(Class&lt;T&gt; cls, ResultSet res) {
  29           HashMap&lt;String, Model&gt; data = new HashMap&lt;String, Model&gt;();
  30           Method m;
  31           try {
  32              m = cls.getMethod("getNew", ResultSet.class);
  33              
  34           } catch(Exception e) {
  35               return null;
  36           }
  37           
  38           
  39           try {
  40               while(res.next()) {
  41                   T obj = (T) m.invoke(null, res);
  42                   data.put(obj.toString(), obj);
  43               }
  44           }
  45           catch(Exception e) {
  46               return null;
  47           }
  48           return data;
  49       }
  50       
  51       @Override
  52       public abstract String toString();
  53       
  54       public static Model getNew(ResultSet res) {
  55           return null;
  56       }
  57  }
  </code></pre>
  
  <p>On line 41 I'm calling the above method <code>getNew()</code>. I end up with <code>null</code>s in the map, both for the keys and the values.</p>
  
  <p>The method on line 28 is called like this</p>
  
  <pre><code>return this.&lt;Course&gt;resultsetMap(Course.class, res);
  </code></pre>
  
  <p>Sorry for all the mess, <strong>I've found the</strong> (stupid, as usual) <strong>cause</strong>: the <code>title</code> of the <code>Course</code> class is left unset.</p>