Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Idiom for checking emptiness of ResultSet

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am backing the resultset to a List. I currently have written code



So do I just check in the service layer (or wherever I call this method) for isEmpty() ?

I was also thinking of this structure:



How do I go about handling empty and null conditions?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first approach is the better/cleaner approach. think about people that will call your method. If they know they will always a list back then they don't need to check for null, just if the list isEmpty.
If you return null when there are no results then you introduce the possibility on nullpointers in the calling code.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks E Armitage. Should the method never return null?

I have in the catch block the following lines that I had seen somewhere else,



Should that null be purged? In case of an exception there is nothing returned and the current thread is stopped. Please let me know your thoughts on this.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how do clients know that there was an exception? It's just being hidden in that method.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you but please bear with me. My ignorance is astounding.

When my Application.java from other package calls this method and I deliberately introduce an error in the method like calling a procedure that does not exist, I get this on my console,



I think I should add a printStackTrace() in there and replace that handler code with it. Is this what is correct way?
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you decide what to do with an exception in the top most layer, the one that the user of the application sees. So if it's a problem with the coding or with the system then you catch the exception and display a message to the user with something like "There was a problem processing your request, please contact the administrator or help desk" and if it's a user input problem then you say something like "Your input was invalid, try again".
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great answer; something to ponder over and keep in mind. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic