I want to throw exception if all fields in object are null, but traditional way for checking on null looks not very smart. What can be another way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT : This uses reflection (java.lang.reflect.Field)
You could create a method within this object to return its valid state :
This way, the method is going to validate each and every field of the class so you don’t need to modifiy the code whenever you add a new field to it.
HOWEVER,
the primary key cannot be null, so you have NOT to validate this field, or any NOTNULL field for that matter.