Search code examples
javaandroidinstantiation

Null Object Reference with Custom Array List. Where to instantiate?


Really hit a wall trying to complete my estimations project. Would like to save a custom ArrayList to sharedPreferences with gson. I need to add the estimate to the estimates (a custom ArrayList). I've read a dozen other similar posts and even conquered a similar problem before. I've been going crazy try to figure out what I'm not instantiating correctly and where I should be doing this instead. I feel like I tried everywhere but I'm obviously missing something.

The problem is with the .add() in the code below.

          saveButton = findViewById(R.id.save);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                estimates.add(newEstimate);
                saveData();
            }
        });

Here's the Activity in full

        
public class EstimateActivity extends AppCompatActivity {
    private RecyclerView eRecyclerview;
    private CartAdapter eAdapter;
    private RecyclerView.LayoutManager eLayoutManager;
    private ArrayList<Inventory> eCartList;
    private TextView clientName, clientEmail, workOrder;
    private Button saveButton;
    private ArrayList<Estimate> estimates;
    private Estimate newEstimate;
    private String name, code, email;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_estimate);
        estimates = new ArrayList<>();
        newEstimate = new Estimate();

        //get intents
        Intent intent = getIntent();
        newEstimate = intent.getParcelableExtra("newEstimate");
        estimates = intent.getParcelableArrayListExtra("estimates");
        eCartList = intent.getParcelableArrayListExtra("cartList");

        //set up textViews
        workOrder= findViewById(R.id.cart_work_order);
        clientName = findViewById(R.id.client_name_cart);
        clientEmail = findViewById(R.id.cart_email);

         name = newEstimate.getClientName();
        code = newEstimate.getWorkOrder();
        email = newEstimate.getClientEmail();

        workOrder.setText(code);
        clientName.setText(name);
        clientEmail.setText(email);

        //connect recyclerView
        eRecyclerview = findViewById(R.id.recyclerview_cart);
        eLayoutManager = new LinearLayoutManager(this);
        eAdapter = new CartAdapter(eCartList);
        eRecyclerview.setLayoutManager(eLayoutManager);
        eRecyclerview.setAdapter(eAdapter);
        newEstimate.setCart(eCartList);

        //Set up save button
        saveButton = findViewById(R.id.save);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                estimates.add(newEstimate);
                saveData();
            }
        });


    }


    private void saveData(){
        SharedPreferences sharedPreferences = getSharedPreferences("share preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(estimates);
        editor.putString("task list", json);
        editor.apply();
    }

}



Solution

  • Estimate, Inventory Class have to implement Parcelable interface.

    According to Android API Reference, You have to describeContents() and writeToParcel(Parcel dest, int flags) correctly.

    and also do null check after intent.getParcelableArrayListExtra().