1

I am totally new to developing Android applications and I do not understand why my app crashes. Here's the code:

public class ProductListActivity extends ListActivity {

private ProductDataSource datasource;
private ListView lv;
private ArrayAdapter<Product> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);
    this.addListenerOnAddNewProductButton();

    datasource = new ProductDataSource(this);
    datasource.open();

    datasource.deleteAllProducts();

    List<Product> values = datasource.getAllProducts();
    lv = (ListView) findViewById(android.R.id.list);

    adapter = new ArrayAdapter<Product>(
            this,
            android.R.layout.simple_list_item_1,
            values );
    lv.setAdapter(adapter);
}

private void addListenerOnAddNewProductButton() {
    Button btn = (Button) findViewById(R.id.add_new);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText addProductInput = (EditText) findViewById(R.id.add_product_text);
            String newProductName = addProductInput.getText().toString();
            Product addedProduct = datasource.createProduct(newProductName);

            ArrayAdapter<Product> adapter = (ArrayAdapter<Product>) getListAdapter();
            adapter.add(addedProduct);

            Toast.makeText(getApplicationContext(), "Product added", Toast.LENGTH_LONG).show();
            addProductInput.setText("");
        }
    });
}

I do not understand why products are added to database and still I am receiving the error telling me I am trying to invoke a method on a null object.

Error:

Attempt to invoke virtual method 'void android.widget.ArrayAdapter.add(java.lang.Object)' on a null object reference

I've already seen similar topic here on SO, but it didn't help me - as you can see, I declared my adapter as a global variable in this class. What's going on?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
TheOpti
  • 1,641
  • 3
  • 21
  • 38
  • can we see your `getListAdapter()` method? – Joey Dalu Oct 30 '16 at 21:36
  • Actually, I didn't implement it. I am doing this tutorial: http://www.vogella.com/tutorials/AndroidSQLite/article.html#tutorial-using-sqlite – TheOpti Oct 30 '16 at 21:55
  • okay well I see removing that method worked for you, cause I was going to suggest that since `adapter` is an instance variable you could to without the method cause it's returning null. – Joey Dalu Oct 31 '16 at 01:57

2 Answers2

2

You need to use the setListAdapter method from ListActivity instead of using the one from ListView.

In onCreate, you can do:

setListAdapter(adapter);

instead of

lv.setAdapter(adapter);
Asapha
  • 643
  • 8
  • 14
  • Indeed, it works.What's the difference between these two methods? – TheOpti Oct 30 '16 at 22:27
  • 1
    lv.setAdapter(adapter) sets the adapter in the ListView not the activity. getListAdapter gets the adapter from the activity, which in your case is null. – Asapha Oct 30 '16 at 22:37
  • @TheOpti Since you have a `ListActivity`, you don't need references to `lv` or `adapter` in your class. You use `getListView()` and `getListAdapter()`, respectively – OneCricketeer Oct 30 '16 at 22:39
0

try:

adapter = (ArrayAdapter<Product>) getListAdapter();

instead of:

ArrayAdapter<Product> adapter = (ArrayAdapter<Product>) getListAdapter();

and change:

private ArrayAdapter<Product> adapter;

to:

public ArrayAdapter<Product> adapter;