rovo89 / XposedBridge

The Java part of the Xposed framework.
3.86k stars 1.1k forks source link

XH.setIntField throws NPE for the class, but only for the second time #245

Closed MPeti1 closed 6 years ago

MPeti1 commented 6 years ago

Here is my code: `@Override protected void beforeHookedMethod(MethodHookParam param) throws Throwable { applicationContext = AndroidAppHelper.currentApplication().getApplicationContext();

            xSharedPrefs.reload();
            String log;
            if (xSharedPrefs.getBoolean("pref_key_soccer_score_reset", false)){
                param.args[1] = 0;
                XposedHelpers.setIntField(param.args[0], "z", 0);
                XposedHelpers.setIntField(param.args[0], "A", (int) XposedHelpers.callMethod(param.thisObject, "d"));
                log = "highScore resetelve";
            } else {
                int newScore = xSharedPrefs.getInt("pref_key_soccer_score", 0);
                if (newScore != 0) {
                    param.args[1] = newScore;
                    log = "eredmény átállítva: " + newScore;
                } else log = "eredmény nincs átállítva, mert a kapott érték 0";
            }

            XposedBridge.log(LOG_TAG + SoccerViewHooks.class.getSimpleName() + log);
            ToastUtils.makeToast(applicationContext, SoccerViewHooks.class.getSimpleName() + ": " + log, 2000);
            super.beforeHookedMethod(param);
        }
    });`

The second XposedHelpers.setIntField() throws an NPE while trying to get the class of the first argument (it's the class). But it only happens for the second time, and the AS debugger says the param.args[0] is not null, nor is it changed.

What is happening here? Why XposedHelpers don't receive that (class) object?

MPeti1 commented 6 years ago

Solution: Can you see that param.thisObject in callMethod? Yes, I forgot to replace it here when i realized it's null if you hooked a static method.. I should get used to rubber duck debugging, because it will happen many times int the future. How did I figure it out? In the Logcat menu in AS, I clicked the first method in the stack trace, and I quickly realized that method is not setIntField, but instead callMethod, where I forgot to replace param.thisObject to param.args[0]