Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3780806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:51:44+00:00 2026-05-19T10:51:44+00:00

It seems like it is not a good idea to catch a nullpointerexception. If

  • 0

It seems like it is not a good idea to catch a nullpointerexception. If that’s the case, why is it thrown by methods? Should it just be caught with Exception?

Also, if I encounter a parameter which is null (can either be primitive type like string or a class with fields), how should I handle it? (assumingly not throw npe)?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:51:44+00:00Added an answer on May 19, 2026 at 10:51 am

    A nullpointerexception usually indicates a programmer error. You won’t catch a null pointer exception if you don’t expect such a programmer error. For instance, say you implement a method myMethod with one parameter of type P.

    void myMethod(P x)
    

    Let’s say you don’t expect the parameter x to be null. In this case you might not check for null. Why? because you assume that the programmer who is using your method will use it properly and won’t pass null. I’m not saying whether you should or shouldn’t check for null in such cases. I’m just trying to answer why sometimes a null pointer exception is thrown by a method and isn’t supposed to be caught. Here’s why: Your method might throw a null pointer exception because someone didn’t use it right (passed null). The programmer who is using your method might know that if the method is being used wrong it might throw a nullpointerexcetption. But even if he uses your method wrong the exception won’t be caught because everyone assumes that the method isn’t misused.

    Your second question: First, a String is an Object, not a primitive type. Second, primitives can’t be null (primitives are ints, doubles, floats, chars, booleans and some others). Now, if you encounter a parameter which is null and it shouldn’t be null (in other words you received an illegal argument) you have some choices:

    • Throw an IllegalArgumentException indicating the parameter is null and your method expects it not to be null.
    • Don’t do anything. Assume your method is being used correctly (in other words, assume no programmer errors).
    • Use assertions: assert(x != null);

    I personally am not a fan of defensive programming and usually prefer the second option. But still, if I want to ensure some invariants on my variables I’m using assertions. But these are just my habits and others probably disagree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems that requirements on safety do not seem to like systems that use AI
Seems like this should be easy or at least documented somewhere, I just cant
A bit new with WPF...It seems like not matter what I change with horizontal
Since Rails is not multithreaded (yet), it seems like a threaded web framework would
Not sure what exactly is going on here, but seems like in .NET 1.1
Is it even possible to perform address (physical, not e-mail) validation? It seems like
Seems like as really simple thing to do, but I just can't track it
This seems like it should be so easy, but I've tried three or four
Seems like a simple enough question but I can't seem to find the answer.
Seems like a simple problem: I have an SVN repo inside our firewall. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.