2017-01-03 88 views
0

我有一個名爲MovieObj對象:陣列上的自定義對象空

public class MovieObj { 
private String title; 
private String plot; 
private String rating; 
private String release; 
private String moviePosterTag; 
// 
public MovieObj(){ 
    title = ""; 
    plot = ""; 
    rating = ""; 
    release = ""; 
    moviePosterTag = ""; 

} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 

    this.title = title; 
} 

public String getPlot() { 
    return plot; 
} 

public void setPlot(String plot) { 
    this.plot = plot; 
} 

public String getRating() { 
    return rating; 
} 

public void setRating(String rating) { 
    this.rating = rating; 
} 

public String getRelease() { 
    return release; 
} 

public void setRelease(String release) { 
    this.release = release; 
} 

public String getMoviePosterTag() { 
    return moviePosterTag; 
} 

public void setMoviePosterTag(String moviePosterTag) { 
    this.moviePosterTag = moviePosterTag; 
}} 

在叫JsonParser一個單獨的類,我檢索我的MoviesObj數組,我初始化中的一些Json的信息,並把它的方法:

public MovieObj[] getMovieObjectsL(String moviesJsonString) throws JSONException { 
    final String MOVIEDB_RESULTS = "results"; 
    final String POSTER_PATH = "poster_path"; 

    JSONObject jsonObject = new JSONObject(moviesJsonString); 
    JSONArray results = jsonObject.getJSONArray(MOVIEDB_RESULTS); 

    MovieObj movieObjs[] = new MovieObj[results.length()]; 

    for (int i = 0; i<results.length();i++){ 
     JSONObject individualMovie = results.getJSONObject(i); 
     String moviePosterTag = individualMovie.getString(POSTER_PATH); 
     movieObjs[i].setMoviePosterTag(moviePosterTag); 

    } 

    return movieObjs; 
} 

此方法被分配給MoviesObj的陣列我有一個的AsyncTask內回到我主要活動:

MovieObj [] movieObjs = null; 

     try { 
      final String BASE_URL = "http://api.themoviedb.org/"; 
      String SEARCH_BY = "3/movie/popular"; 
      final String API_KEY_SEARCH = "?api_key="; 
      final String API_KEY ="blah blah blah"; 
      final String LANGUAGE_PARAM = "&language="; 
      final String LANGUAGE = "en-US"; 
      final String PAGE_PARAM = "&page="; 
      final String PAGE_NUM = "1"; 

      if (sortBy().equals("Top Rated")){ 
       SEARCH_BY = "3/movie/top_rated"; 
      } 

      Uri builtUri = Uri.parse(BASE_URL).buildUpon() 
        .appendEncodedPath(SEARCH_BY + API_KEY_SEARCH + API_KEY + LANGUAGE_PARAM + LANGUAGE + PAGE_PARAM + PAGE_NUM) 
        .build(); 
      URL url = new URL(builtUri.toString()); 

      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = null; 

      inputStream = urlConnection.getInputStream(); 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       Log.v("1", "nothing retrieved"); 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 

      try { 
       while ((line = reader.readLine()) != null) { 
        buffer.append(line + "\n");//Helpful for debugging 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      if (buffer.length() == 0) { 
       Log.v("1", "nothing in bufferedString"); 
      }else{ 
       moviesJsonStr = buffer.toString(); 

       JSONParser jsonParser = new JSONParser(); 


       *****movieObjs = jsonParser.getMovieObjectsL(moviesJsonStr); 

       for (int i = 0; i < movieObjs.length; i++){ 
        Log.v("test", "MOVIES = " + movieObjs[i].getMoviePosterTag()); 
       } 
      } 

每次我運行此我得到我的陣列空指針異常從我的方法JsonParser類中:

NullPointerException: Attempt to invoke virtual method 'void .popularmovies.data_helpers.MovieObj.setMoviePosterTag(java.lang.String)' on a null object reference 

這是爲什麼陣列空???我以爲我在這裏的方法中初始化它:

ArrayList<MovieObj> movieObjs = new ArrayList<MovieObj>(); 
+2

'MovieObj movieObjs [] = new MovieObj [results.length()];'你創建了這個數組,但是當你調用這個數組的時候它是空的。 – Compass

+0

可能的重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – shmosel

+0

[數組空指針異常錯誤]可能的重複(http://stackoverflow.com/questions/15747516/array-null-pointer-exception-error) –

回答

1

您沒有初始化數組中的元素。試試這個:

for (int i = 0; i<results.length();i++){ 
    movieObjs[i] = new MovieObj(); 
    JSONObject individualMovie = results.getJSONObject(i); 
    String moviePosterTag = individualMovie.getString(POSTER_PATH); 
    movieObjs[i].setMoviePosterTag(moviePosterTag); 
} 
+0

哇,我一直盯着代碼整天,我錯過了那簡單的初始化。謝謝,很快就會接受... –