• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking whether object returned is null or not

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the following code.

IN Dao class



In Service class


The data queried for is not present in the db.
I need to check if the returned sap1 object has any data or not.
"sap1==null" doesn't work as the else part runs, and the following exception is raised



This problem can be solved by using boolean, but i need to return the Object type only.
What should i do?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
psdao.isAlreadyRegistered() implies the return value of a boolean this is apparently not the case, as the line after you are comparing it to null. I would rename that method as it is misleading. Since that method is apparently doing a fine by id and not finding a result a Hibernate exception is being thrown. For your code to operate properly you will need to add to you isAlreadyRegistered method the following:



That way if the query does not return results it returns null and your null check in the service class will evaluate correctly.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Bill Gorder
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic