2016-10-04 64 views
0

空指針異常我有getter和setter類moviemodel後ececute顯示異步tsask

我得到這個錯誤

FATAL EXCEPTION: main Process: com.example.shailendra.fulljson, PID: 7251                     
    java.lang.NullPointerException 
    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 
    at android.widget.ListView.setAdapter(ListView.java:480) 
    at .MainActivity$JsonClass.onPostExecute(MainActivity) 

MainActivity。 java的

class MainActivity extends AppCompatActivity { 

    Button clickMe; 
    ListView listView; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView=(ListView)findViewById(R.id.listView); 

     clickMe= (Button)findViewById(R.id.clickMe); 
     clickMe.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       new JsonClass().execute("http://jsonparsing.parseapp.com/jsonData/moviesData.txt"); 
      } 
     }); 
    } 

    class JsonClass extends AsyncTask<String,Void,List<MovieModel>> { 

     ListView listView=(ListView)findViewById(R.id.listView); 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected List<MovieModel> doInBackground(String... params) { 
      HttpURLConnection connection=null; 
      BufferedReader reader=null; 
      MovieModel movieModel = new MovieModel(); 
      MovieModel.Cast cast= new MovieModel.Cast(); 

      try { 
       URL url= new URL(params[0]); 
       connection=(HttpURLConnection)url.openConnection(); 
       connection.connect(); 
       InputStream stream=connection.getInputStream(); 
       InputStreamReader isr= new InputStreamReader(stream); 
       reader =new BufferedReader(isr); 
       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 answerObject = parentArray.getJSONObject(i); 
        movieModel.setMovie(answerObject.getString("movie")); 
        movieModel.setYear(answerObject.getInt("year")); 
        movieModel.setRating((float) answerObject.getDouble("rating")); 
        movieModel.setDirector(answerObject.getString("director")); 
        movieModel.setDuration(answerObject.getInt("duration")); 
        movieModel.settagLine(answerObject.getString("tagline")); 
        movieModel.setImage(answerObject.getString("image")); 
        movieModel.setStory(answerObject.getString("story")); 
        List<MovieModel.Cast> castList= new ArrayList<>(); 
        for(int j=0;j<answerObject.getJSONArray("cast").length();j++) { 
         JSONObject castObject= answerObject.getJSONArray("cast").getJSONObject(j); 

         cast.setName(castObject.getString("name")); 
         castList.add(cast); 

        } 
        movieModel.setCastList(castList); 
        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> s) { 
      super.onPostExecute(s); 

      MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,s); 
      listView.setAdapter(adapter); 

     } 

    } 

    public class MovieAdapter extends ArrayAdapter { 
     public List<MovieModel> movieModelList; 
     private int resource; 
     private LayoutInflater inflater; 
     public MovieAdapter(Context context, int resource, List<MovieModel> objects) { 
      super(context, resource, objects); 
      movieModelList=objects; 
      this.resource=resource; 
      inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 

     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if(convertView==null) { 
       convertView=inflater.inflate(resource,null); 
      } 
      ImageView ivMovieIcon; 
      TextView tvMovie; 
      TextView tvTagLine; 
      TextView tvYear; 
      TextView tvDuration; 
      TextView tvDirector; 
      TextView tvCast; 
      TextView tvStory; 
      RatingBar rbMovieRating; 
      ivMovieIcon =(ImageView)convertView.findViewById(R.id.ivicon); 
      tvTagLine=(TextView)convertView.findViewById(R.id.tvTagLine); 
      tvYear=(TextView)convertView.findViewById(R.id.tvYear); 
      tvDuration=(TextView)convertView.findViewById(R.id.tvDuration); 
      tvDirector=(TextView)convertView.findViewById(R.id.tvDirector); 
      tvStory=(TextView)convertView.findViewById(R.id.tvStory); 
      tvCast=(TextView)convertView.findViewById(R.id.tvCast); 
      rbMovieRating=(RatingBar) convertView.findViewById(R.id.rbMovie); 
      tvMovie=(TextView)convertView.findViewById(R.id.tvMovie); 

      tvCast.setText(movieModelList.get(position).getDuration()); 
      tvDirector.setText(movieModelList.get(position).getDirector()); 
      tvYear.setText("Year: " + movieModelList.get(position).getYear()); 
      tvStory.setText(movieModelList.get(position).getStory()); 
      tvDuration.setText(movieModelList.get(position).getDuration()); 
      tvTagLine.setText(movieModelList.get(position).getTagLine()); 
      tvMovie.setText(movieModelList.get(position).getMovie()); 

      StringBuffer stringBuffer= new StringBuffer(); 

      for(MovieModel.Cast cast: movieModelList.get(position).getCastList()) { 
       stringBuffer.append(cast.getName()+ ", "); 
      } 
      tvCast.setText(stringBuffer); 
      rbMovieRating.setRating(movieModelList.get(position).getRating()/2); 
      return convertView; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
//   new JsonClass().execute("http://jsonparsing.parseapp.com/jsonData/moviesData.txt"); 
// 
//   return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

查看我的回答 –

+0

哪一行出錯? – dipali

+0

line 153 ... listadapter – rishav

回答

0

你得到的答案在列表視圖意味着要麼在S返回null,或者你的數據不是從網上正確解析。從json好奇的概念網站檢查你的json,看看s是否返回null。

-1

改變這在安置自己的方法:

MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,movieModelList); 
      listView.setAdapter(adapter); 

讓我知道了對你的工作或沒有?

+0

沒有... moviemodellist是沒有初始化那裏...我們需要變量在movieadapter類...所以不能被定義兩次 – rishav

+0

如果你可以檢查這個https://www.youtube.com/watch?v=X2mY3zfAMfM – rishav

+0

@rishav好的我會檢查它 –

0

好吧,這可能會發生,因爲您在doInBackground的末尾返回null。

要麼

return movieModelList; 

或初始化你的適配器是這樣的。

MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,movieModelList); 

而我的建議是您Adapter類ArrayList使用不同的變量名,這樣你就不會得到駁倒。

快樂編碼。

+0

沒有工作,我只是想糾正錯誤 – rishav

+0

我試過哪一​​種解決方案? –

+0

都嘗試butcouldnt得到解決方案 – rishav

0

此行可能正在創建異常。

convertView=inflater.inflate(resource,null); 

您可以像這樣替換此行。

convertView=inflater.inflate(resource,parent,false); 

並檢查「s」爲空或不是在「onPostExecute法」

@Override 
protected void onPostExecute(List<MovieModel> s) { 
    if(s!=null && s.size()>0) {  
     MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,s); 
     listView.setAdapter(adapter); 
    } 
    super.onPostExecute(s); 
}