Java if condition check for empty and null values

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

yescobar01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yescobar01Author 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

yescobar01Author 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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.