Skip to main content

Posts

Showing posts with the label nullpointerexception

Is Catching a Null Pointer Exception a Code Smell?

Recently a co-worker of mine wrote in some code to catch a null pointer exception around an entire method, and return a single result. I pointed out how there could've been any number of reasons for the null pointer, so we changed it to a defensive check for the one result. However, catching NullPointerException just seemed wrong to me. In my mind, Null pointer exceptions are the result of bad code and not to be an expected exception in the system. Are there any cases where it makes sense to catch a null pointer exception?

NullPointerException - What to do?

I'm experiencing a NullpointerException with this code: kuglebutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(toggle.isChecked()) { counter1 = counter1 + 10; column1tv.setText("" + counter1); } else { counter1 = counter1 - 2; column1tv.setText("" + counter1); } } }); Can anyone tell me what i'm doing wrong, and maybe how to fix it?