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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:22:56+00:00 2026-06-11T00:22:56+00:00

I am currently working with XML files, and am searching to have a better

  • 0

I am currently working with XML files, and am searching to have a better way to avoid try/catch blocks in a nice way.

Here is the thing. Let’s say I have an XML file.

<A>
    <BB>37</BB>
    <CC>
        <DDD>1</DDD>
    </CC>
</A> 

In fact, I turn this into an object, which means that I can do
myXml.getA() and so on.

In my code, I search a lot for given elements in this object, which means that I have a lot of lines like

int ddd = myXml.getA().getCC().getDDD();

The thing is that some elements may not be there, and for example another XML element can be like that only :

<A'>
    <BB'>37</BB'>
</A'> 

So if I try to get ddd, getCC() raises a NullPointerException.

In the end, I end up coding it like that :

int ddd;
try{
    ddd = myXml.getA().getCC().getDDD();
}
catch (NullPointerException e){
ddd = 0;
}

This works but the code becomes really ugly.
I am searching for a solution to have something like

int ddd = setInt(myXml.getA().getCC().getDDD(), 0);

0 being the default in case the method raises an exception.

Is there a nice way to do that ?

Up to now, I couldn’t find a solution that do not raise errors.

Thx for your help !

EDIT: Just not to get XML related answers.
I showed the xml part for everybody to understand the problem.
In my code, I don’t have access to the XML, but only the object that represents it.

To make it short, what I’d really love is some kind of isNull method to test my getters.

  • 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-06-11T00:22:58+00:00Added an answer on June 11, 2026 at 12:22 am

    This is sort of an annoyance of working with jaxb. in my company, we do enough work with jaxb that it was worth writing an xjc plugin which generated “safe” versions of every getter that were guaranteed to return non-null values for any non-trivial value (immutable instances in the case that a sub-object did not really exist).

    Here’s an example of what our generated model entities look like:

    public class ExampleUser implements Serializable {
        private final static long serialVersionUID = 20090127L;
        @XmlAttribute
        protected String name;
        @XmlAttribute
        protected String email;
        public final static ExampleUser EMPTY_INSTANCE = new ExampleUser() {
            private static final long serialVersionUID = 0L;
            @Override
            public void setName(java.lang.String value) { throw new UnsupportedOperationException(); }
            @Override
            public void setEmail(java.lang.String value) { throw new UnsupportedOperationException(); }
        };
    
        public String getName() {
            return name;
        }
    
        public void setName(String value) {
            this.name = value;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String value) {
            this.email = value;
        }
    }
    
    public class ExampleAccount implements Serializable {
        private final static long serialVersionUID = 20090127L;
        protected ExampleUser user;
        @XmlElement(name = "alias")
        protected List<String> aliases;
        @XmlAttribute
        protected String id;
        @XmlAttribute
        protected String name;
        public final static ExampleAccount EMPTY_INSTANCE = new ExampleAccount() {
            private static final long serialVersionUID = 0L;
            @Override
            public void setUser(com.boomi.platform.api.ExampleUser value) { throw new UnsupportedOperationException(); }
            @Override
            public List<String> getAliases() { return java.util.Collections.emptyList(); }
            @Override
            public void setId(java.lang.String value) { throw new UnsupportedOperationException(); }
            @Override
            public void setName(java.lang.String value) { throw new UnsupportedOperationException(); }
        };
    
        public ExampleUser getUser() {
            return user;
        }
    
        public void setUser(ExampleUser value) {
            this.user = value;
        }
    
        public List<String> getAliases() {
            if (aliases == null) {
                aliases = new ArrayList<String>();
            }
            return this.aliases;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String value) {
            this.id = value;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String value) {
            this.name = value;
        }
    
        public ExampleUser safeGetUser() {
            return (getUser() != null) ? getUser() : ExampleUser.EMPTY_INSTANCE;
        }
    }
    

    So you could write this code without fear of NPE:

    userEmail = account.safeGetUser().getEmail();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have this version of the autocomplete control working when returning XML from
I am currently working on a page that will read in XML files that
Intro Currently I am working with XML files. With PHP I combine several XML
I am currently working on an application that parses huge XML files. For each
I have a Flex app that uploads files to our website. We're currently working
I have been working on code that parses external XML-files. Some of these files
I am currently working with an XML document which has RSS feeds inside. And
I'm currently working with an XML Schema that has <xsd:annotation> / <xsd:documentation> on most
To set the scene, I'm currently working on an XML adapter for Android that
Currently am working on a project that requires me to create some XML for

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.