0

First of all before i created this dao I was doing this operation in a helper class and it was working well. Now I'm getting a NullpointerException on this line where I call dao.

Student stu = pf.getUser(request.getParameter("st_id").toString());

I tried to trace it and find the problem but i couldn't.

I'm new in jdbc so any suggestions will be appreciated.

This is my student dao; program doesn't even enter the getUser method.

public class StDAO extends BaseDAO {

    public Student getUser(String Id) throws Exception {
        String query = "select * from students where id=" + Id;
        ResultSet rs = execQuery(query);
        return new Student(rs.getString("name"), Id, rs.getString("gpa"));
    }

    public void addUser(String id, String name, String gpa) throws Exception {
        String query = "INSERT INTO students (name, id, `gpa`) VALUES ('" + name + "'," + id + "," + gpa + ")";
        ResultSet rs = execQuery(query);
    }
}
Henrique Ordine
  • 3,337
  • 4
  • 44
  • 70
hece
  • 364
  • 2
  • 15
  • 3
    So, when your code doesn't even enters the DAO, why post it and omit the calling code? Speculation seems useless to me. – Smutje Aug 22 '14 at 14:48
  • if it doesn't enter getUser() then either your request object is null or it doesn't contain a parameter "st_id", which will raise the null pointer exception on toString() – kasoban Aug 22 '14 at 14:50
  • I thought maybe my method and method call somehow incompatible to each other. @Smutje – hece Aug 22 '14 at 14:50
  • 1
    Move request.getParameter("st_id").toString() to its own line of code, and assign it to a String variable, and then pass that String variable into pf.getUser. This will help you further narrow down the possible problem. It is much easier to debug code when you simplify each line as much as possible. – dave823 Aug 22 '14 at 15:03
  • Do you have your connection set up properly? – Rika Aug 22 '14 at 15:04
  • see this answer http://stackoverflow.com/a/24100776/217324 for advice on how to read a stacktrace. – Nathan Hughes Aug 22 '14 at 15:29

1 Answers1

0

If you get an NPE, then request is null, or the result of request.getParameter("st_id") is null. There is no connection to a DAO problem, and you need to tell us where request comes from.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112