0

I want load data from server into my application, and when users click button save this post (content) from server into SQLiteDatabse.

I want when go to the activity, fist check database, if exists this post into database change button color.
For this job, I check posts with Title!
That's mean if this post Title into databse setBtnColor="R.color.primaryColor", another side if this post Title NOT into database setBtnCOlor="R.color.black" .

I write below codes, but when running application, show me Force Close error.

DatabaseHelperClass codes:

public boolean checkFavPost(String title) {

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    // 2. set cursor for read row
    Cursor cursor = db.rawQuery("SELECT * FROM " + FavContract.favInfo.TABLE_NAME + " WHERE " +
            FavContract.favInfo.FAV_TBL_PTitle + " = ?", new String[]{title});

    if (cursor.getCount() > 0) {
        return true;
    } else {
        return false;
    }
}

Activity codes:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_show_page);

        bindActivity();

        // Initialize
        postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.post_FavImage);
        post_cover = (ImageView) findViewById(R.id.postShow_cover_image);
        postShow_title = (TextView) findViewById(R.id.postShow_title);
        postShow_title2 = (TextView) findViewById(R.id.postShow_titleBig);
        //postShow_content = (TextView) findViewById(R.id.postShow_content_text);
        postShow_dateTime = (TextView) findViewById(R.id.postShow_man_date_text);
        postShow_author = (TextView) findViewById(R.id.postShow_man_author_text);
        postShow_category = (TextView) findViewById(R.id.postShow_man_category_text);
        title_sliding = (TextView) findViewById(R.id.post_sliding_title);
        comment_Recyclerview = (RecyclerView) findViewById(R.id.comment_recyclerView);
        post_content_web = (WebView) findViewById(R.id.postShow_content_web);

        //Give Data
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            title = bundle.getString("title");
            image = bundle.getString("image");
            content = bundle.getString("content");
            dateTime = bundle.getString("dateTime");
            author = bundle.getString("author");
            category = bundle.getString("category");
            categoryID = bundle.getString("categoryID");
        }

        if (favDB.checkFavPost(title) == true) {
            postShow_favPost.setBtnColor(R.color.colorPrimary);
        } else {
            postShow_favPost.setBtnColor(R.color.black);
        }

Show me error for this line :

if (favDB.checkFavPost(title) == true) {
    postShow_favPost.setBtnColor(R.color.colorPrimary);
} else {
    postShow_favPost.setBtnColor(R.color.black);
}

LogCat error:

09-28 10:38:02.916 2033-2033/com.tellfa.colony E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.tellfa.colony, PID: 2033
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.colony/com.tellfa.colony.Activities.PostShow_page}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
                                                                     at com.tellfa.colony.Activities.PostShow_page.onCreate(PostShow_page.java:122)
                                                                     at android.app.Activity.performCreate(Activity.java:6020)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5349) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

My dear friend, I know this error show NullPointerException, but I am amateur and my question is how can I fix it?!

Please don't give me negative points, please help me. Thanks all <3

Community
  • 1
  • 1
  • Something is not there what you are looking for `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference` and it is `checkFavPost` probably – Murtaza Khursheed Hussain Sep 28 '16 at 07:23
  • the problem is favDB = null – Linh Sep 28 '16 at 07:25
  • @MurtazaKhursheedHussain, Thanks man, can you say me how can I fix it? can you send me code? please man –  Sep 28 '16 at 07:25
  • @PhanVanLinh, Thanks man. it's help me –  Sep 28 '16 at 07:29

2 Answers2

0

Problem is u check the possible value of null

try check it with something like

if(value != null){
//and ur code
}

also learn something about nullpointexception What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Rodriquez
  • 981
  • 1
  • 7
  • 21
0

Looks like you have not initialized the Class.

Please initialize the DatabaseHelperClass with constructor arguments.

DatabaseHelperClass favDB = new DatabaseHelperClass(this);

somia
  • 611
  • 5
  • 22