0

I use startActivityForResult to start another activity to input some information, and then pass the intent back. Here is the partial code extend ListActivity:

private ArrayList<Book> shoppingCart = new ArrayList<Book>();
ViewHolderAdapter viewHolderAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO check if there is saved UI state, and if so, restore it (i.e. the cart contents)

    if (shoppingCart.isEmpty())
        getListView().setEmptyView(findViewById(android.R.id.empty));
    else
    {
        viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart);
        setListAdapter(viewHolderAdapter);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.bookstore_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId())
    {
        case R.id.add:
            Intent intent = new Intent(this, AddBookActivity.class);
            startActivityForResult(intent, ADD_REQUEST);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if(requestCode == ADD_REQUEST) {
        if (resultCode == RESULT_OK) {
        //  Log.d(TAG, "*************************************");
            Bundle data = intent.getExtras();
            Book book = (Book) data.getParcelable("book");
            shoppingCart.add(book);

            viewHolderAdapter.notifyDataSetChanged();
        }
    }

}

However, their is an error:

Attempt to invoke virtual method 'void edu.stevens.cs522.bookstore.activities.BookStoreActivity$ViewHolderAdapter.notifyDataSetChanged()' on a null object reference

this is the button which send intent back:

Button add_btn = (Button) findViewById(R.id.add_one);
    add_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = getIntent();
            EditText titleView = (EditText) findViewById(R.id.search_title);
            EditText authorView = (EditText) findViewById(R.id.search_author);
            EditText isbnView = (EditText) findViewById(R.id.search_isbn);
            EditText priceView = (EditText) findViewById(R.id.search_price);

            String author_str = authorView.getText().toString();
            String[] split_str = author_str.split("\\s+");
            Author author = null;
            if (split_str.length == 2) {
                author = new Author(split_str[0], split_str[1]);
            }
            if (split_str.length == 3) {
                author = new Author(split_str[0], split_str[1], split_str[2]);
            }

            String title = titleView.getText().toString();
            String isbn = isbnView.getText().toString();
            String price = priceView.getText().toString();

            intent.putExtra("book", new Book(title, author, isbn, price));
            setResult(Activity.RESULT_OK,intent);
            finish();
        }
    });

Thanks in advance.

sydridgm
  • 1,012
  • 4
  • 16
  • 30
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – David Medenjak Jan 31 '16 at 00:24
  • 1
    ...`viewHolderAdapter` must be null then. Where are you currently assigning it a value? – George Mulligan Jan 31 '16 at 00:25
  • @GeorgeMulligan I want use it update the ListView. And the list empty at the beginning, so when I pass back an intent, I want it to display the Book object. I add the viewholderadapter code. – sydridgm Jan 31 '16 at 00:42
  • `onActivityResult` happens before `onResume` and `onCreate`, so your adapter is not created yet, if the activity was killed – njzk2 Jan 31 '16 at 00:44
  • We don't need to see all your code `ViewHolderAdapter` code. I don't see where you are ever assigning `viewHolderAdapter` a value. Something like `viewHolderAdapter = new ViewHolderAdapter(...);` – George Mulligan Jan 31 '16 at 00:48
  • It should be like, `if (shoppingCart.isEmpty()) getListView().setEmptyView(findViewById(android.R.id.empty)); else{ viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart); setListAdapter(viewHolderAdapter);}` – user370305 Jan 31 '16 at 01:09
  • @GeorgeMulligan yeah, I do not assign value to it. At the beginning, there is no item in shoppingCart, so I use setEmptyView. However, even if I delete if ... else ... just keep setListAdapter, there will still a same error. – sydridgm Jan 31 '16 at 01:10
  • @sydridgm - You never initiate `viewHolderAdapter` in Activity, so you have to do it in your activity's `onCreate()` else condition part. – user370305 Jan 31 '16 at 01:12
  • @sydridgm - always use `if-else` with braces. – user370305 Jan 31 '16 at 01:16
  • @user370305 I change my code like you said. But it still can not work. and the error is same. – sydridgm Jan 31 '16 at 01:16
  • As I said in my above comment use if-else condition with `{ }`. – user370305 Jan 31 '16 at 01:17

1 Answers1

1

You need to make sure you are always initializing the viewHolderAdapter. Update your onCreate method to the following. Notice that the viewHolderAdapter is always assigned a value. It is also ok to always call setEmptyView because it is only shown if your adapter is empty.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO check if there is saved UI state, and if so, restore it (i.e. the cart contents)
    getListView().setEmptyView(findViewById(android.R.id.empty));
    viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart)
    setListAdapter(viewHolderAdapter);
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50