Is it possible in Java to check if objects fields are null and then add default value to all those attributes?

后端 未结 8 508
天涯浪人
天涯浪人 2020-12-05 06:57

I need to make sure that no object attribute is null and add default value in case if it is null. Is there any easy way to do this, or do I have to do it manually by checkin

8条回答
  •  天命终不由人
    2020-12-05 07:39

    I tried this and it works without any issues to validate if the field is empty. I have answered your question partially as I haven't personally tried to add default values to attributes

    if(field.getText()!= null && !field.getText().isEmpty())
    

    Hope it helps

提交回复
热议问题