tufekoi tufekoi - 9 days ago 5
Java Question

Gson parsing on android with proguard

I only have a parsing problem with Gson when I use proguard. In debug mode and release mode without proguard, everything work fine.

This is my json string :


{"tables":[{"name":"Profile","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"age","type":"integer"},{"label":"human","type":"integer"},{"label":"gear","type":"Gear","custom":true,"list":true}]},{"name":"Gear","columns":[{"label":"id","type":"text","primary":true},{"label":"type","type":"text","enum":true},{"label":"name","type":"text"},{"label":"id_Profile","type":"integer","foreign_key":true}]},{"name":"Animal","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"magic","type":"integer"},{"label":"id_Profile","type":"integer","foreign_key":true}]}]}


These are my POJOs :

public class Database {
private List<Table> tables;

public List<Table> getTables() {
return tables;
}

public void setTables(List<Table> tables) {
this.tables = tables;
}
}

public class Table {
private String name;
private List<Column> columns;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Column> getColumns() {
return columns;
}

public void setColumns(List<Column> columns) {
this.columns = columns;
}
}


I read the file containing the json string and parse it this way :

public Database getDatabase(Context context) throws IOException {
InputStream stream = context.getAssets().open(PATH);
String schema = getJsonSchema(stream);
Gson gson = new Gson();
return gson.fromJson(schema, Database.class);
}


But when I try to call the method
getTables()
from my
Database
object :

for (Table table : database.getTables()) {
//...
}


I receive this error :
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
. I have included those rules for proguard. Any idea what's the reason ?

Answer

As darwin commented, adding the @Keep annotation to my POJOs classes fixed the issue