학교 프로젝트의 'assets'폴더에있는 JSON 파일에서 구문 분석을 시도하고 있습니다. 비슷한 프로젝트를 수행했으며 nullpointerexception을 반환하는 이유를 알지 못합니다. 오류가 그것이 null이라는 목록에서 크기를 가져 오려고한다는 것을 알지만, 내가 실패한 곳을 볼 수는 없다는 것을 이해합니다. 나는이 문제에 어떤 도움을 주셔서 감사합니다, 이것은 나를 미치게합니다.Assets 폴더에서 Json 파일을 구문 분석하는 동안 NullPointerException이 발생했습니다.
public class RSSFeedCategoryContainer {
private List<RSSFeedCategory> rssFeedCategoryList;
public void setRssFeedCategoryList(List<RSSFeedCategory> rssFeedCategoryList) {
this.rssFeedCategoryList = rssFeedCategoryList;
}
public List<RSSFeedCategory> getRssFeedCategoryList() {
return this.rssFeedCategoryList;
}
}
나는 NewsController가 있습니다 : 내가 개체 컨테이너 RSSFeedCategoryContainer이
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;
}
}
: 내가 개체 RSSFeedCategory이
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();
}
}
:
은 내가 MainActivity가
0 1,239,나는 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();
}
}
은 '자산'폴더에있는 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 오류 :
당신의RSSFeedCategoryContainer
에
@SerializedName("results")
private List<RSSFeedCategory> rssFeedCategoryList;
: 63,210
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)
이것이 정답입니다. 사랑해. 내가 '결과'를 변경 한 것을 완전히 놓쳤습니다. 나는 이것을지지했다. 그러나 나는 아직 충분한 명성을 얻지 못했다. 나는 프로그래밍에 익숙하지 않고 하루 종일 머리를 태워서 일을하고 다시하고있다. 고마워요 !!! – Joey
당신은 환영합니다 – x0r