In the below code, line 13 is causing a NullPointerException, and for the life of me, I can't figure out why:
1 byte[] hash;
2 byte[] salt;
3
4 JsonObject jsonObject = new JsonParser().parse(request).getAsJsonObject();
5 String username = jsonObject.get("mUsername").getAsString();
6 char[] password = jsonObject.get("mPassword").getAsString().toCharArray();
7
8 String HQLGetUser = "SELECT User FROM User WHERE mUsername = :username";
9
10 Session session = HibernateUtilities.openSession();
11 session.beginTransaction();
12
13 User user = session.createQuery(HQLGetUser)
14 .setString("username", username)
15 .list();
16
17 session.getTransaction().commit();
18 session.close();
19
20 hash = user.get(0).getHash();
21 salt = user.get(0).getSalt();
The database definitely has a record with the username I'm searhing for. I tried creating a query and calling get(User.class, userId)
with the same session object using the primary key to search for the record instead of the username string, and it returned a User
object, so SessionFactory is definitely being configured. I also queried the database in phpmyadmin using:
select * from user where username = :username
and I also got the record I wanted. Any ideas why I could be getting the NullPointerException?
Update: Here is my user class the code references:
@Entity
@Table(name="user")
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Long mId;
@Column(name="username")
private String mUsername;
@Column(name="hash")
private byte[] mHash;
@Column(name="salt")
private byte[] mSalt;
}