-1

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;
}
the beest
  • 463
  • 6
  • 26
  • Try splitting your statement up into individual calls instead and identify which one of the `setString` or `uniqueResult` calls is on an NPE. Also, do you actually set the `username` outside the piece of code seen here? – radimpe May 09 '16 at 14:19
  • Where do you set `username`? – Riaz May 09 '16 at 14:20
  • @Riaz i added the code where i set the username – the beest May 09 '16 at 14:26
  • @radimpe i tried that and the NPE was being caused on the create query line – the beest May 09 '16 at 14:27
  • 1
    Debugger is your friend. – Tobb May 09 '16 at 14:28
  • Well `session`'s not null or else it would have blown up earlier, so is `username` null? Debug through it. – Riaz May 09 '16 at 14:28
  • So why are you refering to `HQLGetUser` if your query is `SQLGetUser`? – radimpe May 09 '16 at 14:33
  • @radimpe that's a typo i made to the SO post. does not reflect my code. i'll change it – the beest May 09 '16 at 14:35
  • @Tobb yea maybe when it works. netbeans won't get it started. it attempts it for about three minutes, and then shuts down my server showing me a build failed error – the beest May 09 '16 at 14:42
  • @Riaz username is definitely not null. i printed it to the console, and it's exactly how i typed it into the json request – the beest May 09 '16 at 14:43
  • Your HQL is then the problem. Seems you are all over the place with your variable names. Your 'sample' sql query shows `select * from user where username = :username` while your HQL refers to `select User from User` (capitals, named column) as well as `mUsername`. Another post suggest the same: http://stackoverflow.com/questions/6216371/null-pointer-on-hibernates-createquery – radimpe May 09 '16 at 14:45
  • @radimpe i am not all over the place with my variable names. if you read the post, you'd see that the sql was executed in phpmyadmin, and if you know the bare minimum about `Hibernate` you'd know that sql queries use the names of the columns, while hql queries use the field names of the entity class you create which correspond to columns in the table you linked that entitiy to, and that the names of the fields in your entity don't have to match the names of the columns in the database they correspond too – the beest May 09 '16 at 15:00
  • please post your User entity class as well.. – Abdullah Khan May 09 '16 at 15:04
  • @AbdullahWasi done – the beest May 09 '16 at 15:07

2 Answers2

0

Hibernate HQL queries are different from SQL queries and the syntax works differently. Try dropping the "SELECT User" bit and make your query:

FROM User WHERE mUsername = :username

Alternately, you can leave your query as is and change the method call from

session.createQuery

to

session.createSQLQuery

see if that makes a difference.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Riaz
  • 874
  • 6
  • 15
-1

Solved it. All that was necessary was to remove SELECT USER from the hql query.

the beest
  • 463
  • 6
  • 26