2015-12-23 52 views
0

我試圖讓從URL的請求得到JSON解析器和JSON這從機器人的ListView,但是我收到一個錯誤,我的應用程序關閉錯誤上嘗試JSON解析器顯示java.lang.NullPointerException

更多細節,這裏是我的課:

public class JSONTask extends AsyncTask<String, String, List<MovieModel>>{ 


    @Override 
    protected List<MovieModel> doInBackground(String... params) { 

     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null){ 
       buffer.append(line); 
      } 

      String finalJson = buffer.toString(); 

      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.getJSONArray("movies"); 

      List<MovieModel> movieModelList = new ArrayList<>(); 
      for (int i=0; i<parentArray.length(); i++){ 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       MovieModel movieModel = new MovieModel(); 
       movieModel.setMovie(finalObject.getString("name")); 
       movieModel.setYear(finalObject.getInt("year")); 
       movieModel.setRating((float) finalObject.getDouble("rating")); 
       movieModel.setDuration(finalObject.getString("duration")); 
       movieModel.setDirector(finalObject.getString("director")); 
       movieModel.setTagline(finalObject.getString("tagline")); 
       movieModel.setImage(finalObject.getString("image")); 
       movieModel.setStory(finalObject.getString("story")); 

       List<MovieModel.Cast> castList = new ArrayList<>(); 
       for (int j=0; j<finalObject.getJSONArray("stars").length(); j++){ 
        MovieModel.Cast cast = new MovieModel.Cast(); 
        cast.setName(finalObject.getJSONArray("stars").getJSONObject(j).getString("name")); 
        castList.add(cast); 
       } 
       movieModel.setCastList(castList); 
       // Add the final object in the list 
       movieModelList.add(movieModel); 
      } 
      return movieModelList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null){ 
       connection.disconnect(); 
      } 

      try { 
       if (reader != null){ 
        reader.close(); 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(List<MovieModel> result) { 
     super.onPostExecute(result); 

     MovieAdapter adapter = new MovieAdapter(getApplicationContext(), R.layout.row, result); 
     lvMovies.setAdapter(adapter); 



    } 
} 

當我打開我的aplication,他接近,而在我的日誌

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.helio.minhaaplicacao, PID: 9419 
java.lang.NullPointerException 
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 
at android.widget.ListView.setAdapter(ListView.java:480) 
at com.example.helio.minhaaplicacao.MainActivity$JSONTask.onPostExecute(MainActivity.java:185) 
at com.example.helio.minhaaplicacao.MainActivity$JSONTask.onPostExecute(MainActivity.java:106) 
at android.os.AsyncTask.finish(AsyncTask.java:632) 
at android.os.AsyncTask.access$600(AsyncTask.java:177) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5021) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
at dalvik.system.NativeStart.main(Native Method) 

回答

1

返回這是你在你的後臺噸有異常事件hread,你將返回null。這導致將適配器的List設置爲null,當ListView嘗試生成其View時,會引發NPE。

嘗試返回一個空列表。

+0

你能舉個例子嗎?我是一個初學者 –

+0

嘿,我得到了,這是一個缺乏關注,JSON對象數組「故事」不存在,是「sumarry」,但仍然謝謝你 –

相關問題