0

I'm trying to get result of my query from my database with jpa. When the query founds nothing in the database, it returns null pointer exception. I tried to check it if statement like in below code but I still getting null pointer exception.

public List<String> getQuestionSavedUser(String salesOrder, String module,int questionID)
    {
        List<String> singleUser= entityManager.createQuery("select i.savedBy from EVMResponses i where i.salesOrder='"+salesOrder+"' AND i.module='"+module+"' AND i.questionID="+questionID+"", String.class).getResultList(); 
        System.out.println(singleUser.size());
        if(singleUser.size()>0&&singleUser.get(0)!=null)
        {
            users.add(singleUser.get(0));
        }
        else
        {
            users.add("");
        }
        return users;
    }

How can I solve this problem?

1

2 Answers 2

1

try this:

public List<String> getQuestionSavedUser(String salesOrder, String module,int questionID)
    {
        List<String> users = new ArrayList<>();
        List<String> singleUser= entityManager.createQuery("select i.savedBy from EVMResponses i where i.salesOrder='"+salesOrder+"' AND i.module='"+module+"' AND i.questionID="+questionID+"", String.class).getResultList(); 
        System.out.println(singleUser.size());
        if(singleUser.size()>0&&singleUser.get(0)!=null)
        {
            users.add(singleUser.get(0));
        }
        else
        {
            users.add("");
        }
        return users;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

You're right when I forgot to define users, after the defining the problem solved. Thanks.
0

Maybe the entityManager is null,check it!
or you can use:

List<String> singleUser = new ArrayList<>();
singleUser = entityManager.createQuery("select i.savedBy from EVMResponses i where i.salesOrder='"+salesOrder+"' AND i.module='"+module+"' AND i.questionID="+questionID+"", String.class).getResultList(); 
        

to see whether entityManager works well or not. \

3 Comments

The entityManager works fine and it's not null also I tried with this List<String> singleUser = new ArrayList<>(); but I'm still getting error on this code users.add(singleUser.get(0));
seems that you never define the users
i rewrite an answer, it may suit this

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.