java - NullPointerException on getPlacesUrl(params[0]) -
i using included placeprovider.java tutorial regarding setting searchview , integrating google places api. code tutorial seems quite buggy- @ least in terms of integrating existing code. particular error being thrown mainactivity starts. loads second , error thrown:
03-10 00:17:27.716: e/androidruntime(7895): fatal exception: modernasynctask #1 03-10 00:17:27.716: e/androidruntime(7895): java.lang.runtimeexception: error occured while executing doinbackground() ... 03-10 00:17:27.716: e/androidruntime(7895): caused by: java.lang.nullpointerexception 03-10 00:17:27.716: e/androidruntime(7895): @ java.net.urlencoder.encode(urlencoder.java:92) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.getplacesurl(placeprovider.java:266) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.getplaces(placeprovider.java:290) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.query(placeprovider.java:88)
where begin in terms of debugging error? thanks! full class , log cat below. i've commented lines mentioned in logcat.
placeprovider.java:
public class placeprovider extends contentprovider { public static final string authority = "com.....placeprovider"; //removed forum public static final uri search_uri = uri.parse("content://"+authority+"/search"); public static final uri details_uri = uri.parse("content://"+authority+"/details"); private static final int search = 1; private static final int suggestions = 2; private static final int details = 3; // obtain browser key https://code.google.com/apis/console string mkey = "key=my_api_key"; //removed forum // defines set of uris allowed content provider private static final urimatcher murimatcher = buildurimatcher(); private static urimatcher buildurimatcher() { urimatcher urimatcher = new urimatcher(urimatcher.no_match); // uri "go" button urimatcher.adduri(authority, "search", search ); // uri suggestions in search dialog urimatcher.adduri(authority, searchmanager.suggest_uri_path_query,suggestions); // uri details urimatcher.adduri(authority, "details",details); return urimatcher; } @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { cursor c = null; placejsonparser parser = new placejsonparser(); placedetailsjsonparser detailsparser = new placedetailsjsonparser(); string jsonstring = ""; string jsonplacedetails = ""; list<hashmap<string, string>> list = null; list<hashmap<string, string>> detailslist = null; matrixcursor mcursor = null; switch(murimatcher.match(uri)){ case search: // defining cursor object columns description, lat , lng mcursor = new matrixcursor(new string[] { "description","lat","lng" }); // create parser object parse places in json format parser = new placejsonparser(); // create parser object parse place details in json format detailsparser = new placedetailsjsonparser(); // places google places api jsonstring = getplaces(selectionargs); //this line 88 in code try { // parse places ( json => list ) list = parser.parse(new jsonobject(jsonstring)); // finding latitude , longitude each places using google places details api for(int i=0;i<list.size();i++){ hashmap<string, string> hmap = (hashmap<string, string>) list.get(i); detailsparser =new placedetailsjsonparser(); // place details jsonplacedetails = getplacedetails(hmap.get("reference")); // parse details ( json => list ) detailslist = detailsparser.parse(new jsonobject(jsonplacedetails)); // creating cursor object places for(int j=0;j<detailslist.size();j++){ hashmap<string, string> hmapdetails = detailslist.get(j); // adding place details cursor mcursor.addrow(new string[]{ hmap.get("description") , hmapdetails.get("lat") , hmapdetails.get("lng") }); } } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } c = mcursor; break; case suggestions : // defining cursor object columns id, suggest_column_text_1, suggest_column_intent_extra_data mcursor = new matrixcursor(new string[] { "_id", searchmanager.suggest_column_text_1, searchmanager.suggest_column_intent_extra_data } ); // creating parser object parse places in json format parser = new placejsonparser(); // places google places api jsonstring = getplaces(selectionargs); try { // parse places ( json => list ) list = parser.parse(new jsonobject(jsonstring)); // creating cursor object places for(int i=0;i<list.size();i++){ hashmap<string, string> hmap = (hashmap<string, string>) list.get(i); // adding place details cursor mcursor.addrow(new string[] { integer.tostring(i), hmap.get("description"), hmap.get("reference") }); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } c = mcursor; break; case details : // defining cursor object columns description, lat , lng mcursor = new matrixcursor(new string[] { "description","lat","lng" }); detailsparser = new placedetailsjsonparser(); jsonplacedetails = getplacedetails(selectionargs[0]); try { detailslist = detailsparser.parse(new jsonobject(jsonplacedetails)); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } for(int j=0;j<detailslist.size();j++){ hashmap<string, string> hmapdetails = detailslist.get(j); mcursor.addrow(new string[]{ hmapdetails.get("formatted_address") , hmapdetails.get("lat") , hmapdetails.get("lng") }); } c = mcursor; break; } return c; } @override public int delete(uri uri, string selection, string[] selectionargs) { // todo auto-generated method stub return 0; } @override public string gettype(uri uri) { // todo auto-generated method stub return null; } @override public uri insert(uri uri, contentvalues values) { // todo auto-generated method stub return null; } @override public boolean oncreate() { // todo auto-generated method stub return false; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { // todo auto-generated method stub return 0; } /** method download json data url */ private string downloadurl(string strurl) throws ioexception{ string data = ""; inputstream istream = null; httpurlconnection urlconnection = null; try{ url url = new url(strurl); // creating http connection communicate url urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url istream = urlconnection.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(istream)); stringbuffer sb = new stringbuffer(); string line = ""; while( ( line = br.readline()) != null){ sb.append(line); } data = sb.tostring(); br.close(); }catch(exception e){ log.d("exception while downloading url", e.tostring()); }finally{ istream.close(); urlconnection.disconnect(); } return data; } private string getplacedetailsurl(string ref){ // reference of place string reference = "reference="+ref; // sensor enabled string sensor = "sensor=false"; // building parameters web service string parameters = reference+"&"+sensor+"&"+mkey; // output format string output = "json"; // building url web service string url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters; return url; } private string getplacesurl(string qry){ try { qry = "input=" + urlencoder.encode(qry, "utf-8"); //this line 266 } catch (unsupportedencodingexception e1) { e1.printstacktrace(); } // sensor enabled string sensor = "sensor=false"; // place type searched string types = "types=geocode"; // building parameters web service string parameters = qry+"&"+types+"&"+sensor+"&"+mkey; // output format string output = "json"; // building url web service string url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters; return url; } private string getplaces(string[] params){ // storing data web service string data = ""; string url = getplacesurl(params[0]); //this line 290 try{ // fetching data web service in background data = downloadurl(url); }catch(exception e){ log.d("background task",e.tostring()); } return data; } private string getplacedetails(string reference){ string data = ""; string url = getplacedetailsurl(reference); try { data = downloadurl(url); } catch (ioexception e) { e.printstacktrace(); } return data; } }
details logcat:
03-10 00:17:27.716: e/androidruntime(7895): fatal exception: modernasynctask #1 03-10 00:17:27.716: e/androidruntime(7895): java.lang.runtimeexception: error occured while executing doinbackground() 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.modernasynctask$3.done(modernasynctask.java:137) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.futuretask.setexception(futuretask.java:125) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.futuretask.run(futuretask.java:138) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 03-10 00:17:27.716: e/androidruntime(7895): @ java.lang.thread.run(thread.java:1019) 03-10 00:17:27.716: e/androidruntime(7895): caused by: java.lang.nullpointerexception 03-10 00:17:27.716: e/androidruntime(7895): @ java.net.urlencoder.encode(urlencoder.java:92) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.getplacesurl(placeprovider.java:266) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.getplaces(placeprovider.java:290) 03-10 00:17:27.716: e/androidruntime(7895): @ com.....placeprovider.query(placeprovider.java:88) 03-10 00:17:27.716: e/androidruntime(7895): @ android.content.contentprovider$transport.query(contentprovider.java:187) 03-10 00:17:27.716: e/androidruntime(7895): @ android.content.contentresolver.query(contentresolver.java:262) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.cursorloader.loadinbackground(cursorloader.java:49) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.cursorloader.loadinbackground(cursorloader.java:35) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.asynctaskloader.onloadinbackground(asynctaskloader.java:242) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.asynctaskloader$loadtask.doinbackground(asynctaskloader.java:51) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.asynctaskloader$loadtask.doinbackground(asynctaskloader.java:40) 03-10 00:17:27.716: e/androidruntime(7895): @ android.support.v4.content.modernasynctask$2.call(modernasynctask.java:123) 03-10 00:17:27.716: e/androidruntime(7895): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 03-10 00:17:27.716: e/androidruntime(7895): ... 4 more
before pass qry
encode()
method check not null below...
try { if (qry != null) { qry = "input=" + urlencoder.encode(qry, "utf-8"); //this line 266 } } catch (unsupportedencodingexception e1) { e1.printstacktrace(); }
Comments
Post a Comment