1

I am using postgresql for database. And the condition on the field is not null. When user keeps blank for that field and hit on enter its giving null pointer exception becuase in postgresql for integer it wont accept accept "".Any idea about how to handle this case

3
  • Null pointer exception? Are you sure this is exactly what you are getting? Commented Apr 19, 2011 at 14:08
  • What is your development environment? Language? Persistence layer? Commented Apr 19, 2011 at 14:09
  • Before asking how to avoid a problem, you should ask yourself "what do I expect my system to do in this case"? (when a integer field is left blank in an input form). Commented Apr 19, 2011 at 14:12

3 Answers 3

4

If you can modify the INSERT statement, you can use the COALESCE() function to automatically replace a NULL value with another value you specify.

Here is an example to automatically insert 0 (zero) in the status field, when the $status variable is not set:

INSERT INTO foo (name, status) VALUES ($name, COALESCE($status, 0) );

The COALESCE function can take any number of arguments and returns the first non-NULL value it finds. This comes in handy when you have a "waterfall" of values you want to try before settling on the default value. For example:

COALESCE($status1, $status2, $status3, $status4, 0)
Sign up to request clarification or add additional context in comments.

Comments

3

Since your field does not accept NULL, you should set it with an actual integer.

Replace the empty string with a 0 (or other default value) on the client side.

2 Comments

Can you check if I am in the right track?var abc = document.getElementById('abc').value; if(abc == "" ){ abc.value = 0; }
@divya: yes, something like that. But since this is javascript and may be forged, you should additionally sanitize any user input in your server-side db layer.
1

Check your form with javascript, and if is coming a "" from the textbox, just assign 0 to that value. Then pass that value as an Integer value and that's all.

2 Comments

Can you check if I am in the right track?var abc = document.getElementById('abc').value; if("".equals(abc)){ abc.value = 0; }
Yes, you are. It's something like that.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.