If some fields are required when inserting data, should I check if null when retrieving them (suppose I need to do something with this fields that may throw exception if null), or I shouldn't unless I changed my system and started to accept null for these fields ?


3 Answers 3


Your question can be understood in two way:

  1. Should I trust that the database if enforcing the not null restriction?
  2. Should I trust that other code validated not to insert a null value (no database app or the database accepts null but somehow we want to enforce it ourselves)

In case 1.

Trust the database. Both for not null restriction and referential integrity. If you are going to check for null when the database is designed not to accept null in that column you might as well end up validating referential integrity before inserting a row. Let the database work for you.

In case 2.

If allowing null in that piece of data is definitely out of question, let the program fail during tests. That will tell you the part of the program than inserts the row must be fixed. If some other factors out of your control, like network latency causing failing external API calls, could cause the retrieved value to be null then go with the null check.

After all "a programmer is someone who looks both ways before crossing a one-way street"... so they say.


You should not depend on the data being previously validated. Even if it is coming from a database that you know does not allow null in that column, it is still conceivable that there could be some bug in the intermediary code that ends up passing you a null. That said, is there a reason for checking it instead of handling the exception?

Can you do anything about it other than throw an exception? Is there a benefit to throwing an exception now instead of when the program tries to use the null value? Could the null value be used in some way that would corrupt your data or application?

If checking it doesn't in some way create a better outcome than not checking in the case of a null value, then there is no reason to check it.

  • Sorry but why not trust your DB constrains as long as requried is a constrain in my APP and should not allow data with null value to be inserted for this column ? Checking the null in my case will only prevent the app from crashing and allow the workflow of it to continue and this shouldn't be allowed in my case.
    – Dabbas
    Aug 31, 2016 at 6:37

If you ask whether you should check, you should first figure out what you are going to do when something is wrong and the checks fail.

"Throwing an exception" is not the answer - that just passes the buck. At some point you have to decide what to do. And what you do must be better than "hope it doesn't happen and pray the result isn't too bad if it happens", because that's what you get for free without checking.

So unless and until you come up with the answer what you are going to do if the check fails, the check is absolutely pointless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.