taycaldwell / riot-api-java

Riot Games API Java Library
http://taycaldwell.github.io/riot-api-java/
Apache License 2.0
192 stars 73 forks source link

NullPointerException #117

Closed ltdung1997 closed 7 years ago

ltdung1997 commented 7 years ago

I got this exception at line item.getGold().getTotal()); and item.getImage().getFull() of the method below: `public void addItem(Item item) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(TAG_ITEM_ID,item.getId()); values.put(TAG_ITEM_NAME,item.getName()); values.put(TAG_ITEM_TOTALGOLD,item.getGold().getTotal()); values.put(TAG_ITEM_IMAGE,item.getImage().getFull());

    db.insert(TABLE_ITEM, null, values);
}`
Linnun commented 7 years ago

If the Riot API returns NULL (or no value) for "image", then you can't access its subvalues (like "full"). Make sure to check item.getImage() for NULL before calling item.getImage().getFull().

Is there a specific item that causes this issue? It's probably worth checking out the raw API response for that item.

ltdung1997 commented 7 years ago

I just followed the example: ItemList championList = api.getDataItemList(Platform.NA); Map<String,Item> map = ItemList.getData(); for (Item item : map.values()) { // put item as parameter } and all items cause the NullPointerException.

taycaldwell commented 7 years ago

Not all data is returned by the static data endpoints by default intentionally to reduce payload size.

Taken from the implementation notes of the endpoint from the official documentation of the Riot Games API:

Not all data specified below is returned by default. See the tags parameter for more information.

If you want more data than is provided by default you will need to provide a tag using the overloaded method:

getDataItemList(Platform platform, Locale locale, String version, ItemListTags... tags)

In your case you will want to provide ItemListTags.GOLD and ItemListTags.IMAGE or simply ItemListTags.ALL.

Otherwise, all the data that isn't included will be null/false/0 (depending on the data type of the field.)

We can probably document this better, or at least include equivalent documentation to what is provided in the Riot Games API documentation, as I see this question a lot and it is pretty confusing that the method without the optional parameters doesn't return all data by default.

ltdung1997 commented 7 years ago

Thank you very much!