从Assets文件夹解析Json文件时出现NullPointerException

时间:2016-06-27 00:09:25

标签: android json nullpointerexception

我正在尝试从位于'assets'文件夹中的JSON文件解析学校项目。我做了一个类似的项目工作,我不知道为什么它返回nullpointerexception。我理解错误指出我正试图从列表中获取它为空的大小,但我无法看到我失败的地方。我很感激在此事上的任何帮助,这让我发疯。

我有一个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();
    }
}

我有一个对象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;
    }
}

我有一个Object容器RSSFeedCategoryContainer:

public class RSSFeedCategoryContainer {

    private List<RSSFeedCategory> rssFeedCategoryList;

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

    public List<RSSFeedCategory> getRssFeedCategoryList() {

        return this.rssFeedCategoryList;
    }
}

我有一个NewsController:

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

我有一个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;
    }
}

我有一个扩展GenericDAO的NewsDAO:

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

        return rssFeedCategoryContainer.getRssFeedCategoryList();
    }
}

以下是'assets'文件夹中的RSSFeedCategory.json:

{ "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"
    }
] }

logcat错误:

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) 

1 个答案:

答案 0 :(得分:1)

您所要做的就是添加此注释:

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

RSSFeedCategoryContainer。没有它,Gson无法正确地将数组从json映射到rssFeedCategoryList字段