title: Attempt to invoke virtual method 'boolean android.util.SparseBooleanArray.get(int)' on a null object reference
body: <p>I am creating an alert dialog that has some checkedtextviews and I want to take only the checked ones and add them through my Service class to my activity.</p>
  
  <p>Whenever I try to tun it it works fine until I click the ok in the dialog box. I get the following:</p>
  
  <p>Attempt to invoke virtual method 'boolean android.util.SparseBooleanArray.get(int)' on a null object reference</p>
  
  <pre><code>public class DisplayShoppingListDetailsActivity extends AppCompatActivity{
      private ShoppingList list;
      private AlertDialog alert;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          //defines the activity layout
          setContentView(R.layout.shopping_list_details);
  
          ListView listViewDetails = (ListView) findViewById(R.id.shopping_list_details);
  
          Intent intent = getIntent();
          list = (ShoppingList) intent.getSerializableExtra("ShoppingList");
  
          ProductsOnListAdapter ad = new ProductsOnListAdapter(this, -1, Service.getService().getProductsOnList(list));
          listViewDetails.setAdapter(ad);
  
          FloatingActionButton button = (FloatingActionButton) findViewById(R.id.floatingButton);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  createDialog(list);
      }
  
      private void createDialog(final ShoppingList list){
          final AlertDialog.Builder builder = new AlertDialog.Builder(DisplayShoppingListDetailsActivity.this);
          LayoutInflater inflater = this.getLayoutInflater();
          final View dialogView = inflater.inflate(R.layout.list_of_products, null);
  
          final ListView listView = (ListView) dialogView.findViewById(R.id.list_of_products_listView);
          ListOfProductsAdapter adapter = new ListOfProductsAdapter(this, -1, Service.getService().getPromotions());
          listView.setAdapter(adapter);
  
          builder.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                  ArrayList&lt;String&gt; checkedItems = new ArrayList&lt;&gt;();
                  SparseBooleanArray checked = listView.getCheckedItemPositions();
                  for (int i = 0; i &lt;listView.getAdapter().getCount(); i++) {
                      if (checked.get(i)) {
                          String item = String.valueOf(listView.getItemIdAtPosition(checked.keyAt(i)));
                          //checkedItems.add(item);
                          Service.getService().updateProductToList(list, item);
                      }
                  }
              }
          });
  
          builder.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
              }
          });
  
          builder.setView(dialogView);
          alert = builder.create();
          alert.show();
  </code></pre>