Can't call “get” on null value in JAXB generated model

CrsshKacqsSWd

New Member
I am not so familiar to JAXB but this is the context I have, please read everything to understand.1. I am doing the following to get a value using JAXB from this XML...XML Document snippet (Original):\[code\] <Request deploymentMode="production"> <OrderRequest> <OrderRequestHeader orderID="12345" orderDate="2012-07-04" type="new"> <Total> <Money currency="USD">200.0</Money> </Total> <ShipTo> <Address> <Name xml:lang="">Test</Name> <PostalAddress name="default"> <DeliverTo/> <Street>Value 1</Street> <City>Value 2</City> <State>Value 3</State> <PostalCode>302010</PostalCode> <Country isoCountryCode="US">USA</Country> </PostalAddress> </Address> </ShipTo>\[/code\]So, the value I want is the \[code\]State\[/code\] from \[code\]PostalAddress\[/code\] and I use this code:\[code\]String state = xml.getRequest().getOrderRequest().getOrderRequestHeader().getShipTo().getAddress().getPostalAddress().getState();//This will return "Value 3"\[/code\]And everything works good.2. The problem starts at this point, I will do the same as above but using now this XML...XML Document snippet (Other):\[code\] <Request deploymentMode="production"> <OrderRequest> <OrderRequestHeader orderID="12345" orderDate="2012-07-04" type="new"> <Total> <Money currency="USD">200.0</Money> </Total> <ShipTo> <Address> <Name xml:lang="">Test</Name> </Address> </ShipTo>\[/code\]Well, I know that the value doesn't exist but I don't want to make any change to my code and still using this: \[code\]String state = xml.getRequest().getOrderRequest().getOrderRequestHeader().getShipTo().getAddress().getPostalAddress().getState();\[/code\]The above code will produce a \[code\]NullPointerException\[/code\] and stop the process. What I need is not to stop the process cause this is not the only value I am extracting from XML (this is just an example of what I am dealing with). So, If the value can't be found, then I want that my \[code\]state\[/code\] variable be \[code\]null\[/code\] automatically.Q: Is there a way for not producing and exception and make my string variable \[code\]state\[/code\] just \[code\]null\[/code\] if the value can't be found? PS. A possible solution could be to do something like this:\[code\]//Fill the entityPostalAddressType p = xml.getRequest().getOrderRequest().getOrderRequestHeader().getShipTo().getAddress().getPostalAddress();//Variable for stateString state = null;//Evaluate if it is null or notif(p != null) //Capture the value if not null state = p.getState();\[/code\]But I don't like to make an \[code\]if\[/code\] statement for every entity of my XML. As I said, I want something automate without modifying the code and I don't know if there is some JAXB configuration or something to do for this.Thanks in advance.
 
Back
Top