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

  • SEARCH
  • Home
  • 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 7584737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:59:35+00:00 2026-05-30T18:59:35+00:00

When writing a method, say, inside one of your DAO objects, and you dont

  • 0

When writing a method, say, inside one of your DAO objects, and you dont want this method to accept certain input, for discussions sake, say it does not allow null arguments. How do you go about implementing that, taking into account this method is likely to be reused in the future by new team members.

The way I do it is:

  1. In the interface, I document inside the method javadoc that arguments a, b and c cannot be null.
  2. Inside the method I check for null values first thing, and if any of a, b or c are null then I throw an IllegalArgumentException.

But, what if some developer in the future just reads off the method signature and decides that it what he/she needs and starts using it, without paying attention to this detail, and worse testing does not reveal it. NULL pointer exception won’t occur and we are getting a useful error message, but we are still getting an error in production that could’ve been avoided.

Is there a way to enforce this at compile time? I doubt it, but what would be the safest and most bad-developer-proof way to go about doing this?

  • 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-30T18:59:37+00:00Added an answer on May 30, 2026 at 6:59 pm

    I don’t think you can enforce this compile time, but you can certainly make the method signatures easy to understand.

    If you don’t mine adding a dependency on one of the validations frameworks you can use JSR 303‘s @NotNull or similar tools like Preconditions.

    With JSR 303 you can do suff like:

    @NotNull
    @Size(min = 2, max = 14)
    @UpperCase
    private String licensePlate;
    

    or

    @NotNull
    @NotEmpty
    public String foo(@NotNull @Pattern(regexp="[0-9]") String param) {
       ...
    }
    

    Have a look at the Getting started with JSR 303 Bean Validation for more comprehensive examples.

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

Sidebar

Related Questions

Let's say I'm writing a jQuery extension method. This method should climb the ancestor
Say you're writing method foo() in class A. foo doesn't ever access any of
Lets say I have the below method called DoSomething. When writing unit tests how
Let's say I have a method, Foo() . There are only certain times when
I'm writing a method which, let's say, given 1 and hello should return http://something.com/?something=1&hello=en
Alright, so let's say I'm writing a forum application, and I want pretty URLs.
Say I have a method like this*: public T GetItem(int index) { if (index
I have a doubt about orkut's new URL Writing method. Earlier the home page
When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something
I am writing an method that takes variable lengths of arrays and compresses and

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.