exception - Why does Java have NullPointerException's? -


why java throw exception when call method on null object? understand, exceptions indicate 'recoverable' error. however, can't see situation wrap method call in try block, , catch nullpointerexception. couldn't check whether object null before making method call? seems if try call method on null reference, program should abort , crash immediately, since indicates programmer error rather 'recoverable' error.

from understand, exceptions indicate 'recoverable' error.

well, that's interpretation , exception in question. in many cases, cannot much.

but if cannot much, don't want crash whole program. logging (or ignoring) error , moving on appropriate response here.

however, can't see situation wrap method call in try block, , catch nullpointerexception.

why not?

couldn't check whether object null before making method call?

sure. that's extremely common thing do.

unfortunately, it's extremely common thing forget. indeed programming error. programming errors need handled gracefully, when common.

it seems if try call method on null reference, program should abort , crash immediately,

that's exceptions used for.

the caller can decide if want try or crash.

since indicates programmer error rather 'recoverable' error.

that interpretation , special case. programmer cannot held responsible arguments supposed (per api spec) not-null being null, example.


a better question maybe "why java have null?".

because of problems causes (programs crashing npe or worse behaviour), null has been called billion dollar mistake, , newer languages scala or rust try fix not allowing null values (instead have explicitly wrap value in optional structure).


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -