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 6904297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:02:31+00:00 2026-05-27T08:02:31+00:00

For my Java application, I am creating an instance of a user information object

  • 0

For my Java application, I am creating an instance of a user information object and populating it with a service that I don’t control the source for.

The code looks like this:

// username given as parameter
UserInfo ui = new UserInfo();
try {
    DirectoryUser du = LDAPService.findUser(username);
    if (du!=null) {
       ui.setUserInfo(du.getUserInfo());
    }
} catch (Exception e) {
    // Whatever
}

If LDAPService.findUser() can’t locate a user, it will throw a NullPointerException and grind the rest of my application to a stop. It’s okay if the user information isn’t populated, so I want to be able to continue without causing everything else to start throwing exceptions.

Is there a way to do 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-27T08:02:31+00:00Added an answer on May 27, 2026 at 8:02 am

    I’ve upvoted Amir Afghani’s answer, which seems to be the only one as of yet that actually answers the question.

    But I would have written it like this instead:

    UserInfo ui = new UserInfo();
    
    DirectoryUser du = null;
    try {
        du = LDAPService.findUser(username);
    } catch (NullPointerException npe) {
        // It's fine if findUser throws a NPE
    }
    if (du != null) {
       ui.setUserInfo(du.getUserInfo());
    }
    

    Of course, it depends on whether or not you want to catch NPEs from the ui.setUserInfo() and du.getUserInfo() calls.

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

Sidebar

Related Questions

I'm creating a Java application in NetBeans, and I have the following code that
I'm creating a Java application where the user can search through a list of
I am new to Documentum. I am creating a stand alone java application that
I've creating a Java Swing application and I realized that I have many many
I'm currently creating a fullscreen Java application that will be like a kiosk application.
I am creating a Java application were i have a paragraph that i want
I'm creating a Java application that uses Hunch's API , but the problem is
I am creating an application in java which will be the part of an
When creating a new project in netbeans, if i select JAVA Desktop application, it
My Java application is saving stuff in 'user.home' but on windows this does not

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.