I have an object called 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;
}}
In a separate class called JsonParser, I have a method that retrieves some Json information and places it within my an Array of MoviesObj that I initialized:
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;
}
This method is assigned to an array of MoviesObj that i have within an ASyncTask back in my main activity:
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());
}
}
Every time I run this I get a null Pointer Exception on my Array from my method within the JsonParser class:
NullPointerException: Attempt to invoke virtual method 'void .popularmovies.data_helpers.MovieObj.setMoviePosterTag(java.lang.String)' on a null object reference
Why is this array null??? I thought I initialized it within the method here:
ArrayList<MovieObj> movieObjs = new ArrayList<MovieObj>();