1

I'm getting Null Pointer Exception while retrieving Primary Key or Id of an Entity in Hibernate result query. The Primary Key value is simply a long value. My entity class:

@Entity
@Table(name="sample")
public class ccy{

@Id
@column(name = "S_ID")
private long id;

2nd varibale and so on

when i write hibernate query to retrieve it causes Null pointer exception, my select query is:

 long ID = 0;
 String HQL = "select c.id from ccy c where c.name='sowndhar' ";
 Query query = session.createQuery(HQL);
 ID = (Long)query.uniqueResult(); //getting NullPointerException in this    line
 return ID;

Any suggestions plz? Any help would be much appreciated

6
  • What's the stack trace? Commented Mar 20, 2016 at 18:33
  • 1
    Obviously query must be null. So you might want to check if something with your session is wrong. Or (as I dont know about hibernate): could it be that you first have to execute your query? It seems a bit suspicious that you create a query; to then immediately try to access its result. Besides: ccy is a really bad name for a class. Should start with uppercase; and: do not abbreviate. Code exists to be read; and such abbreviations dont tell much. Commented Mar 20, 2016 at 18:44
  • Share your complete code where you are creating session and starting transaction. I doubt you have some issue with session. Another point I noticed that you are expecting Unique result from a query without a where clause. If your code worked and there are multiple records, then it will give you org.hibernate.NonUniqueResultException error. Also as already stated, your naming convention is very confusing. Commented Mar 20, 2016 at 19:02
  • why should select id from sample return an Long, I would guess that it returns a List of Longs, or du you only have 1 entry in your database? Commented Mar 20, 2016 at 19:37
  • 1
    I think using "sample" in HQL does not work as "sample" is the table name, but HQL needs entity/class name (ccy in your case) Commented Mar 20, 2016 at 19:40

1 Answer 1

1

There are several bad Java practices in your code. By convention class names begin with a capital letter. Variable names begin with a lower case letter. Constants use all caps. Follow the naming conventions.

In the following code I change the type of your variable so that it allows null as a value. I also add some error handling that you will definitely want when requesting a unique result.

By the way, are you sure your session variable is not null? How are you getting it?

    Long id = null; // use the nullable Java type, not the primitive

    String HQL = "select c.id from Ccy c where c.name=:name";

    try {
        // always use parameter tokens in your query ... for security
        Query query = session.createQuery(HQL)
            .setParameter("name", name);

        id = (Long)query.uniqueResult(); 
    } catch (NonUniqueResultException e) {
        // what to do if there are multiple matches
    } catch (NoResultException e) {
        // what if there is no match
    }

    return id;
Sign up to request clarification or add additional context in comments.

3 Comments

session I'm getting it from sessionFactory
Are you sure that it is not null?
Yes that is not getting null as i'm injecting sessionFactory using spring dependency injection. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.