title: Exception in thread "main" java.lang.NullPointerException at database project
body: <p>Im trying to see all the data in my database or insert a data to my database. Both methods are giving the same response.
  my pojo class:</p>
  
  <pre><code>public class Title {
  
  private Integer id;
  private String name;
  private String code;
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getCode() {
      return code;
  }
  
  public void setCode(String code) {
      this.code = code;
  }
  
  }
  </code></pre>
  
  <p>my service class for add and show methods:</p>
  
  <pre><code>public abstract class TitleService {
  
   public static void addTitle(Title title) throws Exception {
  
      Connection c = Global.createConnection();
      //Title title=new Title();
      PreparedStatement ps = c.prepareStatement(
              "insert into Title (id, name, code) values (?, ?, ?)");
      ps.setInt(1, title.getId());
      ps.setString(2, title.getName());   
      ps.setString(3, title.getCode());
  
      ps.executeUpdate();
      c.commit();
      ps.close();
      c.close();
    }
  public static List&lt;Title&gt; allTitles() throws SQLException {
      Connection c = Global.createConnection();
      Statement st = c.createStatement();
      ResultSet rs = st.executeQuery("SELECT * FROM Title");
      List &lt;Title&gt; titles = new ArrayList&lt;&gt;();
      Title title;
          while (rs.next()) {
              title = new Title();
              title.setId(rs.getInt(1));
              title.setName(rs.getString(2));
              title.setCode(rs.getString(3));
              titles.add(title);
          }
  
      st.close();
      c.close();
      return titles;
      }
  }
  </code></pre>
  
  <p>test class:</p>
  
  <pre><code>public class ServiceTest {
  
  
  public static void main(String[] arga) throws SQLException, Exception {
  
      Title t = new Title();
      t.setId(1);
      t.setCode("Prof");
      t.setName("Profesor");
  
      TitleService.addTitle(t);
  
      //TitleService.allTitles();
  
  }
  
  }
  </code></pre>
  
  <p>when ı run the test class it gives,</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  
  at service.TitleService.addTitle(TitleService.java:22)
  
  at test.ServiceTest.main(ServiceTest.java:26)
  
  Java Result: 1
  
  TitleService.java:22 ---&gt; PreparedStatement ps = c.prepareStatement(
                  "insert into Title (id, name, code) values (?, ?, ?)");
  
  ServiceTest.java:26)---&gt; TitleService.addTitle(t);
  this exception.
  </code></pre>
  
  <p>I dont know what the problem is</p>
  
  <p>my global class foe database connection:</p>
  
  <p>public abstract class Global {</p>
  
  <pre><code>public static Connection createConnection() {
       Connection conn;
      System.out.println("Checking if Driver is registered with DriverManager.");
  try{
          Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException e){
          System.out.println("Couldn't find the driver!");
          System.out.println("exit.");
          System.exit(1);
  }
  System.out.println("make a connection.");
  try{
          conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/uni_db", "postgres", "postgres");
          System.out.println("Success.");
  } catch(SQLException e){
          System.out.println("Couldn't connect: exit.");
          System.exit(1);}
      return null;
  }    
  </code></pre>
  
  <p>}</p>