java - how to ignore the exception and continue to next value -
hi trying pass list of values google api.if api throws exceptions, comes out of loop.i need coninue next value if throws error.my code below.
while (iterator.hasnext()) { object element = iterator.next(); string postcode = element.tostring().trim(); string latlongs[] = getlatlongpositions(postcode); system.out.println("latitude: " + latlongs[0] + " , longitude: " + latlongs[1]); system.out.println(postcode); } public static string[] getlatlongpositions(string address) throws exception { int responsecode = 0; string api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + urlencoder.encode(address, "utf-8") + "&sensor=true"; url url = new url(api); httpurlconnection httpconnection = (httpurlconnection) url.openconnection(); httpconnection.connect(); responsecode = httpconnection.getresponsecode(); if (responsecode == 200) { documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder();; org.w3c.dom.document document = builder.parse(httpconnection.getinputstream()); xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); xpathexpression expr = xpath.compile("/geocoderesponse/status"); string status = (string) expr.evaluate(document, xpathconstants.string); if (status.equals("ok")) { expr = xpath.compile("//geometry/location/lat"); string latitude = (string) expr.evaluate(document, xpathconstants.string); expr = xpath.compile("//geometry/location/lng"); string longitude = (string) expr.evaluate(document, xpathconstants.string); return new string[] { latitude, longitude }; } else { throw new exception("error api - response status: " + status); } } return null; }
even if mention return null instead of throw new exception.it shows null pointer exception.any appreciated.
put try/catch around part
... try{ string latlongs[] = getlatlongpositions(postcode); system.out.println("latitude: " + latlongs[0] + " , longitude: " + latlongs[1]); system.out.println(postcode); }catch(exception e){ ... }
Comments
Post a Comment