-4

I saved an object (Account) in SharedPreferences using gson, but after accessing the object in a new Activity, the instance of the object (newAccount) causes my use of newAccount.returnName() to give a NullPointerException. The error is: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.cashtrack.kennethlee.cashtrack.Account.returnName()' on a null object reference. I understand what a NullPointer error is, but I don't see why I might be getting it since I'm new to Android Studio. The code for the first activity is:

package com.cashtrack.kennethlee.cashtrack;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class CreateAccount extends AppCompatActivity {

    SharedPreferences accountsPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_account);
        setTitle("Create New Account");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        accountsPref = getPreferences(MODE_PRIVATE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_create_account, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //back button
            case android.R.id.home:
                // app icon in action bar clicked; go home
                Intent intent = new Intent(this, MainScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;

            //save button
            case R.id.action_menu_save:

                // get EditText by id
                EditText inputTxt1 = (EditText) findViewById(R.id.AccountName);
                // Store EditText in Variable
                String name = inputTxt1.getText().toString();

                EditText inputTxt2 = (EditText) findViewById(R.id.StartingBalance);
                double balance = Double.parseDouble(inputTxt2.getText().toString());

                Account newAccount = new Account(name, balance);

                SharedPreferences.Editor editor = accountsPref.edit();
                Gson gson = new Gson();
                String json = gson.toJson(newAccount);
                editor.putString("newAccount", json);
                editor.commit();

                Intent intent2 = new Intent(this, MainScreen.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent2);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

The code for the activity that is accessing the object and causing the error is:

package com.cashtrack.kennethlee.cashtrack;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.FileInputStream;
import java.io.StringWriter;
import java.util.ArrayList;

public class MainScreen extends AppCompatActivity {
    //array list of accounts
    ArrayList<Account> accounts = new ArrayList<>();

    SharedPreferences accountsPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainScreen.this, CreateAccount.class));
            }
        });
        accountsPref = getPreferences(MODE_PRIVATE);

        //  PRINT TO A TEXT BOX
        //setContentView(R.layout.activity_main_screen);
        //TextView textView = (TextView) findViewById(R.id.textView4);
        //textView.setText(hello.returnName());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();

        Gson gson = new Gson();
        String json = accountsPref.getString("newAccount", "");
        Account newAccount = gson.fromJson(json, Account.class);
        accounts.add(newAccount);


        setContentView(R.layout.activity_main_screen);
        TextView textView1 = (TextView) findViewById(R.id.textView5);
        textView1.setText(accounts.get(0).returnName());

        TextView textView2 = (TextView) findViewById(R.id.textView6);
        textView2.setText(Double.toString(accounts.get(0).returnAvailableBalance()));
    }
}
Community
  • 1
  • 1
Kenny Lee
  • 3
  • 2

3 Answers3

0

I think you must initialize your Account class in onCreate().

Without initialization it is causing null pointer exception.

In Account class you have created constructor with Context parameter?

maulik
  • 140
  • 9
  • I initialized within the case because I get the name and balance variables for the object in there, so I have to initialize after I get the two variables. I also don't have a context parameter in the account class constructor. What is the purpose of that? – Kenny Lee Feb 19 '17 at 09:34
0

You're using MODE_PRIVATE to specify the file name for storing the value. The recommended way is to use by the default mode, without specifying the file name. Like this:

Put value:

accountPref = PreferenceManager.getDefaultSharedPreferences(this);
Gson gson = new Gson();
String json = gson.toJson(Account);
accountPref.edit().putString("newAccount", json).apply();

Get Value:

SharedPreferences accountsPref = PreferenceManager.getDefaultSharedPreferences(this);
if (accountsPref.contains("newAccount")) {
    Gson gson = new Gson();
    String json = accountsPref.getString("newAccount", "");
    Account newAccount = gson.fromJson(json, Account.class);
    accounts.add(newAccount);
} 

Hope you like this. :)

tahsinRupam
  • 5,820
  • 1
  • 15
  • 33
0

I think that your are having this issues because

getPreferences(int mode)

return a SharedPreferences object for the current Activity, so the String "newAccount" saved in the CreateAccount activity is not retrieved in the MainScreen activity.

So when you use Account newAccount = gson.fromJson(json, Account.class); newAccount is null because json is a empty String.

You may consider use getSharedPreferences(String sharedPreferenceName, int mode) instead of getPreferences(int mode), using the same sharedPreferenceName when you call the method in your activities.

Sorry for my english. I hope it's help.

Cochi
  • 2,079
  • 2
  • 10
  • 15