Java if condition check for empty and null values

yescobar01
yescobar01 used Ask the Experts™
on
How can I write a condition in java for a webservice request, In my webservice request I am passing an element that has `SpeedInfo`. In `SpeedInfo` element there is two sub elements `Bandwidth` and `AccessSpeed`.

This is my Request In SoapUI:

         <sch:SpeedInfo>
            <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:Bandwidth>
            <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:AccessSpeed>
         </sch:SpeedInfo>

Open in new window

My Java condition:

			if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){
				throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
			}

Open in new window

           

I need my condition to check for 3 scenarios:
     1. if <speedInfo> itself is not present

     2. <sch:SpeedInfo> is present, but bandwidth not present:
  
      <sch:SpeedInfo>
          <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
      </sch:SpeedInfo>

     3. Bandwidth is present but no value  
       <sch:SpeedInfo>
          <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1"></ns:Bandwidth>
          <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
       </sch:SpeedInfo>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Depending on exactly how your request gets marshalled into Java objects, the following might be what you want...

if (request.getSpeedInfo() == null || request.getSpeedInfo().getBandwidth() == null) {
    throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
}

Open in new window


If there are issues with the above, post the code for your SpeedInfo class.

Author

Commented:
so will have to test a request where bandwidth is empty.

Bandwidth is an Integer type.
   public Integer getBandwidth() {
        return bandwidth;
    }

Open in new window



this is one of the scenarios that I will be testing on my request:
  <sch:SpeedInfo>
         		<ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1"></ns:Bandwidth>
			<ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">1000000</ns:AccessSpeed>
		</sch:SpeedInfo>

Open in new window

Author

Commented:
this is the error I am getting

org.apache.cxf.interceptor.Fault: Unmarshalling Error: cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'. 
	at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:908)
	at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:712)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial