This is an archived page. The information on this archived page is no longer current.

What is causing the Exception in LiveAddress Java code?

Question

Last Updated: March 18, 2013

I am trying to determine if my customer will be able to use your web service for a web application that I have written. I have simply set up a test using the sample Java code that you supply on your website. The code is shown below along with the error message that I get. I am not sure what the problem is. Any help would be appreciated.

instate = "MI";
incity = "Farmington";
inzip = "45678";
inaddr1 = "23382 Commerce";
inaddr2 = "";
incounty = "OK";
inlastline = "";

webservice.verifyservice.AddressRequest address = new webservice.verifyservice.AddressRequest();
address.setStreet( inaddr1 );
address.setStreet2( inaddr2 );
address.setCity( incity );
address.setState( instate );
address.setZipCode( inzip );
address.setUrbanization( incounty );
address.setLastLine( inlastline );

webservice.verifyservice.ArrayOfAddressRequest addresses = new webservice.verifyservice.ArrayOfAddressRequest();
addresses.setAddressRequest(new webservice.verifyservice.AddressRequest[] { address });

webservice.verifyservice.ServiceRequest request = new webservice.verifyservice.ServiceRequest();
request.setAddresses(addresses);
request.setKey("--hidden--");

webservice.verifyservice.verifyservice.VerifyServiceClient client = new webservice.verifyservice.verifyservice.VerifyServiceClient();

webservice.verifyservice.ServiceResponse response = null;

try
{
	response = client.execute(request);
}
catch (Exception ex)
{
	System.out.print(ex);
}

if (response.isSuccess()
	&& response.getAddresses() != null
	&& response.getAddresses().getArrayOfAddressResponse()[0] != null)
{
	webservice.verifyservice.Address adr = response.getAddresses().getArrayOfAddressResponse()[0].getAddressResponse()[0];

	System.out.println (adr.getStreet());
	System.out.println (adr.getStreet2());
	System.out.println (adr.getCity());
	System.out.println (adr.getState());
	System.out.println (adr.getZipCode());
	System.out.println (adr.getUrbanization());
}

System.out.println(Boolean.toString(response.isSuccess()));
System.out.println(response.getMessage());

if (response.getAddresses().getArrayOfAddressResponse()[0] != null)
{
	System.out.println(Integer.toString(response.getAddresses().getArrayOfAddressResponse()[0].getAddressResponse().length));
}
else
{
	System.out.println("0");
}

Here's the stack trace:

java.rmi.RemoteException: Runtime exception; nested exception is:
deserialization error: unexpected XML reader state. expected: END but found: START: {https://api.smartyst
	reets.com/Address/V1/}Nowjava.lang.NullPointerException
at com.datanat.mahle.unit_tests.TestSmartyStreetsWebService.testWebService(TestSmartyStreetsWebService.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Answer

Here's the offending line of code:

if (response.isSuccess()
	&& response.getAddresses() != null
	&& response.getAddresses().getArrayOfAddressResponse()[0] != null)

There are 3 conditions being checked here and it is the third that is causing the exception. The problem is that when an address can't be verified the array of address responses is empty. So, trying to index the array to get the first entry ([0]) is causing the NullPointerException. For this test case you'll probably want to change the third condition to check to see that the length of the array is greater then 0. You'll also want to create another test case with an address that can be successfully verified - try this for input:

instate = "DC";
incity = "Washington";
inzip = "20500";
inaddr1 = "1600 Pennsylvania Ave NW";
inaddr2 = "";
incounty = "";
inlastline = "";

The leader in location data intelligence

Ready to get started?