0

I have a record that exists in my database which I crossed several times and the record is correct. I have also ensured that toString is overriden in my class to get a meaningful representation of the data I am retrieving.

This is the hql I am using to fetch records from the database

public List <Admin> getByAdminRole(int id) {
        Query query = _sessionFactory.getCurrentSession()
                .createQuery("select a.id from Admin a WHERE a.id = :id");
        query.setParameter("role", id);

        return  query.list();
      }

In my controller class I am calling the hql method this way

SupportDao _supportDao = new SupportDao();
            List <Admin> add = _supportDao.getByAdminRole(1); //this line of code throws null pointer exception

            System.out.println(">>>>>>>>>>> the value of add>>>>>>> " + add);

After research on solving nullpointer exception, I have ensured that toString is overriden in my model class Admin.java

@Override
    public String toString(){

        return this.id + this.name + this.getEmail() + this.getPassword() + this.role;
    }

Please what could be wrong with my code?

4
  • Please post whole stacktrace Commented Oct 7, 2016 at 12:22
  • Which line is SupportDao.java:140? can you indicate this in the code? Commented Oct 7, 2016 at 12:25
  • You are also trying to convert List<Integer> into List<Admin> and also change query.setParameter("role", id); to query.setParameter("id", id); Commented Oct 7, 2016 at 12:28
  • what result you want List of Admins or just Admin Commented Oct 7, 2016 at 12:47

3 Answers 3

2

change

query.setParameter("role", id);

to

query.setParameter("id", id);

if want to return List<Admin> also modify ,

.createQuery("select a.id from Admin a WHERE a.id = :id");

to

.createQuery("from Admin a WHERE a.id = :id");

it will return Admin list with only one Admin if single id is present

if you want single Admin then

return query.uniqueResult(); and change return type as per requirement

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

Comments

1

Since you're instantiating SupportDAO yourself are you absolutely, 101% sure that _sessionFactory in SupportDAO is also instantiated?

2 Comments

And the same with getCurrentSession()?
@Francis still you need to change setParameter statement and casting
0

Looking to your above code where you passing the "id" as parameter but in setParameter method your are passing with "role" .

I guess you need to pass it with "id" only.

Comments

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.