android - java.lang.RuntimeException: An error occured while executing doInBackground() in Popmovie app -
i beginner in android development . trying make movies app got error . here code
public class moviefragment extends fragment { public moviefragment() { } // arrayadapter<string> movieadapter; movieadapter movieadapter; gridview gridview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //add line in order fragment handle menu events. sethasoptionsmenu(true); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { // inflate menu; adds items action bar if present. inflater.inflate(r.menu.moviefragment, menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_refresh) { fetchmovietask movietask = new fetchmovietask(); movietask.execute(); return true; } return super.onoptionsitemselected(item); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); /* movieadapter = new arrayadapter<string>(getactivity(), r.layout.gridview_item, r.id.item_imageview, new arraylist<string>()); */ movieadapter = new movieadapter(getactivity()); gridview = (gridview)rootview.findviewbyid(r.id.gridview); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { movie movie = movieadapter.getitem(position); //toast.maketext(getactivity(),moviecast,toast.length_short).show(); intent intent = new intent(getactivity(),detailactivity.class) .putextra("movie", movie); //.putextra(intent.) startactivity(intent); } }); return rootview; } public class fetchmovietask extends asynctask<void, void,list<string>>{ private final string log_tag = fetchmovietask.class.getsimplename(); protected list<string> doinbackground(void... params){ httpurlconnection urlconnection = null; bufferedreader reader = null; string moviejsonstr = null; try{ string baseurl = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc"; string apikey = "&api_key=" + buildconfig.movie_db_org_api_key; url url = new url(baseurl.concat(apikey)); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if(inputstream==null) return null; reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while((line = reader.readline()) !=null) { buffer.append(line + "\n"); } if(buffer.length()==0) return null; moviejsonstr =buffer.tostring(); // log.v(log_tag , "movie json string: " + moviejsonstr); } catch (ioexception e){ log.e(log_tag, "error ", e); return null; } { if (urlconnection != null) urlconnection.disconnect(); if (reader != null) { try { reader.close(); } catch (final ioexception e) { log.e(log_tag, "error closing stream", e); } } } try { return getmoviedatafromjson(moviejsonstr); }catch (jsonexception e){ log.e(log_tag,"json error",e); } return null; } private list<string> getmoviedatafromjson(string jsonstring) throws jsonexception{ jsonobject moviejson = new jsonobject(jsonstring); jsonarray moviearray = moviejson.getjsonarray("results"); arraylist<movie> movies = null; list<string> urls = new arraylist<string>(); // list<string> arr = new arraylist<string>(); for(int i=0; i<moviearray.length(); i++){ jsonobject indexobject = moviearray.getjsonobject(i); movie indexmovie = new movie(); indexmovie.setoverview(indexobject.getstring("overview")); indexmovie.setreleasedate(indexobject.getstring("release_date")); indexmovie.setposterpath(indexobject.getstring("poster_path")); indexmovie.setvoteaverage(indexobject.getint("vote_average")); // urls.add("http://image.tmdb.org/t/p/w185" + movie.getstring("poster_path")); /* arr.add("http://image.tmdb.org/t/p/w185" + movie.getstring("poster_path")); arr.add("http://image.tmdb.org/t/p/w185" + movie.getstring("overview")); arr.add("http://image.tmdb.org/t/p/w185" + movie.getstring("release_date")); arr.add("http://image.tmdb.org/t/p/w185" + movie.getdouble("vote_average"));*/ //urls.add(arr); movies.add(indexmovie); // log.v(log_tag , "movie json string: " + indexmovie); urls.add("http://image.tmdb.org/t/p/w185" + indexmovie.getposterpath()); } for(string s: urls){ log.v(log_tag, "movie entry : " + s); } return urls; } @override protected void onpostexecute(list<string> strings) { movieadapter.replace(strings); gridview.setadapter(movieadapter); //super.onpostexecute(s); } } private class movieadapter extends baseadapter{ private final string log_tag = movieadapter.class.getsimplename(); private context context; arraylist<movie> movielist; private final list<string> urls = new arraylist<string>(); // private final list<string> urlslist = new arraylist<string>(); public movieadapter(context context){ this.context= context; // collections.addall(urls,posterpath); } @override public int getcount() { return movielist.size(); } @override public movie getitem(int position) { return movielist.get(position); } /* public string getbaseitem(int position){ return urlslist.get(position); }*/ @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if(convertview == null){ convertview = new imageview(context); } imageview imageview = (imageview) convertview; movie moviedb = getitem(position); string url = "https://image.tmdb.org/t/p/w185" + moviedb.getposterpath(); // string url2 = getbaseitem(position); // log.e(log_tag, " url " + url); picasso.with(context).load(url).into(imageview); return convertview; } public void replace(list<string> urls){ if(urls!=null){ this.urls.clear(); this.urls.addall(urls); notifydatasetchanged(); } } } }
my code showing error written below:
e/androidruntime: fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:299) @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) @ java.util.concurrent.futuretask.setexception(futuretask.java:124) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:856) caused by: java.lang.nullpointerexception @ com.example.android.movies.moviefragment$fetchmovietask.getmoviedatafromjson(moviefragment.java:200) @ com.example.android.movies.moviefragment$fetchmovietask.doinbackground(moviefragment.java:169) @ com.example.android.movies.moviefragment$fetchmovietask.doinbackground(moviefragment.java:112) @ android.os.asynctask$2.call(asynctask.java:287) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:856)
there uninitialized arraylist movies
in method getmoviedatafromjson()
so when call movies.add(indexmovie);
cause npe, solve change:
arraylist<movie> movies = null;
to this
arraylist<movie> movies = new arraylist<>();
in method getmoviedatafromjson()
Comments
Post a Comment