0

I am attempting to create a REST application which will work with a HSQLDB. Upon attempting to connect to the DB, i get the following error:

https://i.gyazo.com/341073dc7fea672472f691298373df63.png

The code which the error is pointing to :

public List<Team> getTeams() {
  Connection connection = getConnection();
List<Team> TeamsList = new ArrayList<Team>();

try
{
    PreparedStatement psmt = connection.prepareStatement("SELECT * FROM TEAMS");
    ResultSet rs = psmt.executeQuery();

    while (rs.next())
    {
        int id = rs.getInt("id");
        String league = rs.getString("league");
        String name = rs.getString("name");
        String manager = rs.getString("manager");

        Team team = new Team(id, league, name, manager);
        TeamsList.add(team);
    }
} catch (SQLException e){
     e.printStackTrace();
}
return TeamsList;
        }

Line 74 is PreparedStatement psmt = connection.prepareStatement("SELECT * FROM TEAMS");

When I attempt to execute the HSQL statement from Java it returns null, but when i do it directly to the database (outside of Java) it returns the tables requested.

  • Post the exception stack trace, as text, in the question itself. Not as a link to an image. And remember that we can't guess wta is the number of a line. – JB Nizet Apr 14 '16 at 22:40
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – JB Nizet Apr 14 '16 at 22:52
  • I know what a null pointer is, the problem is that the query shouldn't return null, as when tested in the HSQL line it works, but when i attempt to execute it from java, it returns null. – user3524014 Apr 14 '16 at 22:54
  • No, it seems you don't. The NPE is not caused by the query returning null. It's caused by `connection` being null. So, read the answers to the linked question again. You should also learn to use a debugger. That would make the problem even more immediately obvious. – JB Nizet Apr 14 '16 at 22:56

0 Answers0