A
A
Alexander Shibaev2016-04-10 23:40:35
Android
Alexander Shibaev, 2016-04-10 23:40:35

What is the cause of this error?

Hello.
I send a POST request to the server, the server returns a json of the form in the following format:

{"status":"OK","token":"5de99ea9a1bb550c02a8c2c74e79ac485707dce021cd2b021c8b4567"}

Further, in theory, the value of token should be written to shared preferences:
if (json != null) {

                    try {

                        String jsonstr = json.getString("status");
                        if (json.has("status")) {
                            String token = json.getString("token");
                            SharedPreferences.Editor editor = sharedPreferences.edit(); // 70 строчка на которую ссылается лог
                            editor.putString("token", token);
                            editor.apply();
                            Intent intent = new Intent(Login.this, Main.class);
                            startActivity(intent);
                            finish();
                        }

But at this moment, the application crashes, I certainly assume that my curvature is to blame. Here are my logs, has anyone had a similar problem?
04-10 20:19:30.576 32229-32446/com.dpa7dujijiepgmail.application E/JSON: {"status":"OK","token":"117248e6234b67a6aaa96489ea5fba735707dce021cd2b021c8b4567"}n
04-10 20:19:30.576 32229-32229/com.dpa7dujijiepgmail.application W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa62c9288)
04-10 20:19:30.580 32229-32229/com.dpa7dujijiepgmail.application E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              java.lang.NullPointerException
                                                                                  at com.dpa7dujijiepgmail.application.Login$1.onClick(Login.java:70)
                                                                                  at android.view.View.performClick(View.java:4084)
                                                                                  at android.view.View$PerformClick.run(View.java:16966)
                                                                                  at android.os.Handler.handleCallback(Handler.java:615)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                  at android.os.Looper.loop(Looper.java:137)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
04-10 20:19:33.244 32229-32229/com.dpa7dujijiepgmail.application I/Process: Sending signal. PID: 32229 SIG: 9

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Zagaevsky, 2016-04-10
@Dpa7DuJIJIep

Well, since there is only one call in this line - sharedPreferences.edit(); and we see NullPointerException, then the output is banal - sharedPreferences == null. Fix this and you'll be fine.

G
GavriKos, 2016-04-10
@GavriKos

sharedPreferences == null, obviously. getSharedPreferences did?

A
Alexander Shibaev, 2016-04-11
@Dpa7DuJIJIep

Yes, indeed, I don’t know how I forgot not to do getSharedPreferences. Thanks for answers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question