-7

I'm getting the following JSON response from IMDB.

{
 "Search":
    [
      {
       "Title":"Seven Pounds",
       "Year":"2008",
       "imdbID":"tt0814314",
       "Type":"movie",
       "Poster":"someUrl"
      },
     {
       "Title":"Seven Samurai",
       "Year":"1954",
       "imdbID":"tt0047478",
       "Type":"movie",
       "Poster":"someUrl"
     }
    ],
    "totalResults":"1048",
    "Response":"True"
}

I'd like to extract every movie and store it into a List so I've created a class MovieContainer with a List of Movies, where each Movie contains String attributes describing details about said movie e.g. title, year yiddi yadda - you get the drill!

I used the following code snippet to;

MovieContainer cnt = new Gson().fromJson(jstring, MovieContainer.class);

where jstring is a valid json string similar to the json string sample above, but when I try to iterate over the List in the MovieContainer instance I get a NullPointerException.

I'm new to GSON hence not sure what's the cause?

EDIT: I do know what a NullPointerException is, but I don't understand why Java throws it in my example.

My MovieContainer class:

public class MovieContainer {
    public List<Movie> movies;
}

My Movie class:

public class Movie {
    String Title;
    String Year;
    String Poster;
    String imdbID;
    String Type;
}

I'm expecting the call to the fromJson method to fill my List with the information matching the fields' name, but the List movies points is null.

mtotowamkwe
  • 2,407
  • 2
  • 12
  • 19
Fodark
  • 1
  • 2
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – DontKnowMuchBut Getting Better Dec 29 '16 at 13:53
  • 1
    Please [edit] your question to include a [mcve]. – Jonny Henly Dec 29 '16 at 13:54
  • Learn to read JSON (go to json.org). The outermost object is a dictionary with a single element named "Search". – Hot Licks Dec 29 '16 at 13:56
  • @HotLicks The outermost dictionary has 3 elements, "Search", "totalResults" and "Response". – Erwin Bolwidt Dec 29 '16 at 14:56
  • @ErwinBolwidt - Correct. I missed those with the poor formatting. – Hot Licks Dec 29 '16 at 15:23
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. – Hot Licks Dec 29 '16 at 15:24
  • Any question about an exception should include the exception information. – Hot Licks Dec 29 '16 at 15:25

2 Answers2

0

If you want to search for titles first you need to get "Search" inside that data you need to iterate and look for titles.

Because your Gson contains 3 elements, "Search", "totalResults" and "Response".

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
0

Your MovieContainer class is missing the other two fields i.e. totalResults and Response which are also part of the root json object.

Here's a quick dirty example to get you up and running. It's based on the information you have provided thus far.

Movie.java

public class Movie {
    private String Title;
    private String Year;
    private String imdbID;
    private String Type;
    private String Poster;
}

MovieContainer.java

public class MovieContainer {
    private List<Movie> Search;
    private String totalResults;
    private String Response;

    public static void main(String[] args) {
        // Converts the json to the Java object a.k.a POJO !!!
        deserialize();
    }

    private static void deserialize() {
        String jstring = " { " +
                "  'Search' : [ " +
                "    { " +
                "      'Title' : 'Seven Pounds', " +
                "      'Year' : '2008', " +
                "      'imdbID' : 'tt0814314', " +
                "      'Type' : 'movie', " +
                "      'Poster' : 'someUrl' " +
                "    }, " +
                "    { " +
                "      'Title' : 'Seven Samurai', " +
                "      'Year' : '1954', " +
                "      'imdbID' : 'tt0047478', " +
                "      'Type' : 'movie', " +
                "      'Poster' : 'someUrl' " +
                "    } " +
                "  ], " +
                "  'totalResults' : '1048', " +
                "  'Response' : 'True' " +
                " } ";

         Gson gson = new Gson();

         MovieContainer searchResults = gson.fromJson(jstring, MovieContainer.class);
     }
 }

A screenshot to confirm the List isn't null are as follows:-

IMDB Search Results JSON to Java using GSON

Now time to party see ya!!!

mtotowamkwe
  • 2,407
  • 2
  • 12
  • 19