0

I'm developing a simple e-book reader that reads books based on JSON format.After parsing half of the book i found out that there is problem with some of chars in JSON string,for example:

"[',",# and so on]"

This is my Log:

09-27 14:13:26.781: E/AndroidRuntime(5562): FATAL EXCEPTION: main
09-27 14:13:26.781: E/AndroidRuntime(5562): Process: com.tehedligmail.gencmuslimkitabxana, PID: 5562
09-27 14:13:26.781: E/AndroidRuntime(5562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tehedligmail.gencmuslimkitabxana/com.tehedligmail.gencmuslimkitabxana.ReadingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread.-wrap11(ActivityThread.java)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.os.Handler.dispatchMessage(Handler.java:102)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.os.Looper.loop(Looper.java:148)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread.main(ActivityThread.java:5417)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at java.lang.reflect.Method.invoke(Native Method)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-27 14:13:26.781: E/AndroidRuntime(5562): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
09-27 14:13:26.781: E/AndroidRuntime(5562):  at com.tehedligmail.gencmuslimkitabxana.ReadingActivity.onCreate(ReadingActivity.java:167)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.Activity.performCreate(Activity.java:6237)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
09-27 14:13:26.781: E/AndroidRuntime(5562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

This is my Json string:

    <string name="test_book">{"book_name":"\u001E;'k;'kl","chapter_count":"1","chapters":[{"hadith_name":"\u0259\u0131\u011F\u00F6ı.\u015Fə","hadith_count":"4","hadithes":["\u015F\u001E. !@","\u001E|;]]","|\u001E']'","@$% "]}]}</string>

This is the method that parses content of book and constructs Hadith.class:

    private void prepareHadithData(int ChapterIndex,String BookContent) throws JSONException {


    int HadithCount = 0;
    JSONObject HadithInfo = null;
    String BookName = null;
    String HadithChapterName = null;

    try {

        JSONObject Book = new JSONObject(BookContent);

        BookName = Book.getString("book_name");

        int ChapterCount = Integer.parseInt(Book.getString("chapter_count"));

        JSONArray ChaptersArray = Book.getJSONArray("chapters");

        HadithInfo = ChaptersArray.getJSONObject(ChapterIndex);

        HadithChapterName = HadithInfo.getString("hadith_name");

        HadithCount = Integer.parseInt(HadithInfo.getString("hadith_count"));



    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < HadithCount; i++) {

        String Hadithes = HadithInfo.getJSONArray("hadithes").get(i).toString();


        //Hadith class
        Hadith hadith = new Hadith(BookName, HadithChapterName, (i + 1) + "", Hadithes);

        hadithList.add(hadith);


    }


    //For ListView
    mAdapter.notifyDataSetChanged();



}

As i said,from my observation the main reason of exception may be related to chars from string that can not be parsed.Thanks in advance... PS: it is not dublicate of this question: Because my method is working with string that doesn't contain (' " #) and other non-alphabetic chars.The problem is with some chars i want to replace them or other solution...

Tərlan Əhəd
  • 179
  • 3
  • 10
  • 2
    Post your error log – Nongthonbam Tonthoi Sep 27 '17 at 14:03
  • I have added log,why it is marked as dublicate because this question has no answer in stackoverflow. – Tərlan Əhəd Sep 27 '17 at 14:17
  • Because your error logs clearly shows that your are calling `getString` on a null object. Check the line number and do null check before calling `getString` – Nongthonbam Tonthoi Sep 27 '17 at 14:21
  • Hey man,my method is working like a charm with this json string: {"book_name":"Alphabet","chapter_count":"2","chapters":[{"ha‌​dith_name":"A","hadi‌​th_count":"1","hadit‌​hes":["asda"]},{"had‌​ith_name":"B","hadit‌​h_count":"1","hadith‌​es":["asda"]}]} There no problem with my method man... If you can answer my question please,answer if you can't just go please... @Nongthonbam Tonthoi – Tərlan Əhəd Sep 27 '17 at 14:57

0 Answers0