Joey Joey - 5 months ago 11
JSON Question

NullPointerException while parsing Json file from Assets Folder

I'm trying to parse from a JSON file located on 'assets' folder for a School Project. I have done a similar project that worked and I have no idea why it returns a nullpointerexception. I understand that the error points out that I'm trying to get the size from a list that it is null, but I can't see where I'm failing. I appreciate any help at the matter, this is driving me crazy.

I have a MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

NewsController newsController = new NewsController();
List<RSSFeedCategory> rssFeedCategories = newsController.getRSSFeedCategoryList(this);
Integer rssFeedCategoryAmount = rssFeedCategories.size();
}


}

I have an Object RSSFeedCategory:

public class RSSFeedCategory {
private String categoryName;
private String objectId;

public RSSFeedCategory(String categoryName) {
this.categoryName = categoryName;
}

public String getCategoryName() {
return categoryName;
}

public String getObjectId() {
return objectId;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public void setObjectId(String objectId) {
this.objectId = objectId;
}


}

I have an Object container RSSFeedCategoryContainer:

public class RSSFeedCategoryContainer {

private List<RSSFeedCategory> rssFeedCategoryList;

public void setRssFeedCategoryList(List<RSSFeedCategory> rssFeedCategoryList) {
this.rssFeedCategoryList = rssFeedCategoryList;
}

public List<RSSFeedCategory> getRssFeedCategoryList() {

return this.rssFeedCategoryList;
}


}

I have a NewsController:

public class NewsController {
public List<RSSFeedCategory> getRSSFeedCategoryList(Context context){
NewsDAO newsDAO = new NewsDAO();
return newsDAO.getRSSFeedCategoryList(context);
}


}

I have a GenericDAO:

public class GenericDAO {
public Object getObjectJSON(Context context, Class aClass, String fileName){

Object object = null;
try{

AssetManager manager = context.getAssets();
InputStream inputStream = manager.open(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
Gson gson = new Gson();
object = gson.fromJson(bufferedReader, aClass);
}
catch (Exception e){
e.printStackTrace();
}

return object;
}


}

And I have a NewsDAO that extends the GenericDAO:

public class NewsDAO extends GenericDAO {
public List<RSSFeedCategory> getRSSFeedCategoryList(Context context){
RSSFeedCategoryContainer rssFeedCategoryContainer = (RSSFeedCategoryContainer) getObjectJSON(context,RSSFeedCategoryContainer.class,"RSSFeedCategory.json");

return rssFeedCategoryContainer.getRssFeedCategoryList();
}


}

Here is the RSSFeedCategory.json that is on the 'assets' folder:

{ "results": [
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Design",
"createdAt": "2015-02-03T08:55:30.775Z",
"objectId": "1ezmQEpd1i",
"updatedAt": "2015-02-03T08:55:30.775Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Technology",
"createdAt": "2015-02-03T08:55:29.894Z",
"objectId": "5dBaQy4wpf",
"updatedAt": "2015-02-03T08:55:29.894Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Music",
"createdAt": "2015-02-03T08:55:31.560Z",
"objectId": "73nctHMpkY",
"updatedAt": "2015-02-03T08:55:31.560Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Business",
"createdAt": "2015-02-03T08:55:30.465Z",
"objectId": "C4iDiuriB8",
"updatedAt": "2015-02-03T08:55:30.465Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Gadgets",
"createdAt": "2015-02-03T08:55:30.057Z",
"objectId": "FGTeskscsC",
"updatedAt": "2015-02-03T08:55:30.057Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Auto",
"createdAt": "2015-02-03T08:55:32.000Z",
"objectId": "J2YJqOxGIK",
"updatedAt": "2015-02-03T08:55:32.000Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Gaming",
"createdAt": "2015-02-03T08:55:31.405Z",
"objectId": "J4Oq3C7nxg",
"updatedAt": "2015-02-03T08:55:31.405Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Social",
"createdAt": "2015-02-03T08:55:32.170Z",
"objectId": "N6H0v01tCl",
"updatedAt": "2015-02-03T08:55:32.170Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Cooking",
"createdAt": "2015-02-03T08:55:31.849Z",
"objectId": "XzGdFekl2A",
"updatedAt": "2015-02-03T08:55:31.849Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Entertainment",
"createdAt": "2015-02-03T08:55:31.247Z",
"objectId": "Zk3DzKrF8k",
"updatedAt": "2015-02-03T08:55:31.247Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Fashion",
"createdAt": "2015-02-03T08:55:30.934Z",
"objectId": "ghJX0ATWfq",
"updatedAt": "2015-02-03T08:55:30.934Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Travel",
"createdAt": "2015-02-03T08:55:32.487Z",
"objectId": "hpPkcHkTTV",
"updatedAt": "2015-02-03T08:55:32.487Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Finance",
"createdAt": "2015-02-03T08:55:30.621Z",
"objectId": "nA3FO06l9z",
"updatedAt": "2015-02-03T08:55:30.621Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Photography",
"createdAt": "2015-02-03T08:55:31.087Z",
"objectId": "qC1VFzLgbF",
"updatedAt": "2015-02-03T08:55:31.087Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "News",
"createdAt": "2015-02-03T08:55:29.727Z",
"objectId": "uXFSxpJeDO",
"updatedAt": "2015-02-03T08:55:29.727Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Sports",
"createdAt": "2015-02-03T08:55:30.263Z",
"objectId": "yjHwfD1z3Y",
"updatedAt": "2015-02-03T08:55:30.263Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"categoryName": "Humor",
"createdAt": "2015-02-03T08:55:32.320Z",
"objectId": "z8zyfeKhk7",
"updatedAt": "2015-02-03T08:55:32.320Z"
}


] }

The logcat error:

06-26 20:46:35.161 5957-5957/ar.com.thomas.parsetest E/AndroidRuntime: FATAL EXCEPTION: main
Process: ar.com.thomas.parsetest, PID: 5957
java.lang.RuntimeException: Unable to start activity ComponentInfo{ar.com.thomas.parsetest/ar.com.thomas.parsetest.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at ar.com.thomas.parsetest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

x0r x0r
Answer

All you have to do is to add this annotation:

@SerializedName("results")
private List<RSSFeedCategory> rssFeedCategoryList;

to your RSSFeedCategoryContainer. Without it Gson cannot properly map an array from the json to the rssFeedCategoryList field