0

I have a problem with saving data to SharePrefernce when I forget about my memory card SharePrefernce and error.

my log

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novum.smrtkarta.SharedPreference.saveCardToSharedPreference(android.content.Context, java.util.ArrayList)' on a null object reference
                                                                     at com.xxx.smrtkarta.activity.ScanQrCodeActivity$3$1.run(ScanQrCodeActivity.java:285)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     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)

class save data Card to SharePrefernce ScanQrCodeActivity.java

Default app = (Default) getApplicationContext();
 cardList = app.getListCard();
 Card card = new Card(numberCard, path3, base32, OTP, nameCard, intervalTotp, getDate(), expirationDate);

  if (containsCard(cardList, numberCard, expirationDate)) {
               showAlterDialog("Błąd tworzenia karty", "Podana karta  istnieje w telefonie.");
       } else {
                       cardList.add(card);
                       app.setListCard(cardList);
                       cardList = new ArrayList<Card>();
                       cardList = app.getListCard();

                        //   Toast.makeText(getApplicationContext(),"Rozmair listy"+cardList.size(),Toast.LENGTH_SHORT).show();
                        sharedPreference.saveCardToSharedPreference(mContext,cardList); //Here comes the error message

SharedPreference.java

public class SharedPreference {

    public static final String PREFS_NAME = "LIST_CARD";
    public static final String CARD = "CARD";

    public SharedPreference() {
    }

    public void saveCardToSharedPreference(Context context, ArrayList<Card> cardList) {

        SharedPreferences settings;
        SharedPreferences.Editor editor;

        settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = settings.edit();
        Gson gson = new Gson();
        String jsonString = gson.toJson(cardList);
        editor.putString(CARD, jsonString);
        editor.commit();
    }
}

Default.java

public class Default extends Application {

    public ArrayList<Card> listCard = new ArrayList<Card>();

    public String uuidString;


    public String getUuidString() {
        return uuidString;
    }

    public void setUuidString(String uuidString) {
        this.uuidString = uuidString;
    }

    public ArrayList<Card> getListCard() {
        return listCard;
    }

    public void setListCard(ArrayList<Card> listCard) {
        this.listCard = listCard;
    }

    @Override

    public void onCreate() {
        super.onCreate();

        uk.co.chrisjenx.calligraphy.CalligraphyConfig.initDefault(new uk.co.chrisjenx.calligraphy.CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );

    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Ewelina
  • 69
  • 1
  • 10
  • 3
    So you did not google for NullPointerException! Blame on you. – greenapps May 04 '17 at 11:44
  • as @greenapps said, it's the first thing to do with an exception, google it to understand it, then read the stacktrace to find the reason. But I upvote this since the question is still well written and provide every including the exception ! – AxelH May 04 '17 at 11:45
  • 1
    your custom object `sharedPreference` is null in `ScanQrCodeActivity.java` ......you have to initialize it... – Opiatefuchs May 04 '17 at 11:46
  • You asked a similar question [here](http://stackoverflow.com/questions/43272508/object-is-null-set-in-defalut-add-card-in-arraylist) but it seems you did not take the time to understand the problem and blindly just ask it again. – Denny May 04 '17 at 11:47
  • 1
    by the way, don´t name a custom class like an Android class. `SharedPreferences` already exists, it could be confusing for you anytime... – Opiatefuchs May 04 '17 at 11:48

1 Answers1

0
cardList.add(card);
app.setListCard(cardList);

cardList = new ArrayList();

cardList = app.getListCard();

comment above bold code from your code and try.

Nilesh Patel
  • 419
  • 1
  • 7
  • 15