Note that there are some explanatory texts on larger screens.

plurals
  1. POArray Adapter NotifyDataSetChanged() NullPointerException Android
    text
    copied!<p>I am adding a map to a list and then refreshing the array adapter. This was working perfectly earlier, but now that I am using an addItem() method from two different methods, it throws a NullPointer. I hope my code will clear up what I am saying:</p> <pre><code>SimpleAdapter adapter; List&lt;HashMap&lt;String, String&gt;&gt; painItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); ListView listthings; int[] to; String[] from; String painLevelString, timeOfPainString, textTreatmentString, painLocation, row1, row2, name; OnCreate(){ if(getIntent().getStringExtra("newPainLevel")!= null){ createNewEditedEntry(); } adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, from, to); listthings.setAdapter(adapter); } private void createNewEditedEntry() { String newPainLevel = this.getIntent().getStringExtra("newPainLevel"); String newPainTime =this.getIntent().getStringExtra("newPainTime"); String newTreatment =this.getIntent().getStringExtra("newTreatment"); painLevelString = newPainLevel; timeOfPainString = newPainTime; textTreatmentString = newTreatment; row1 = "sample1"; row2 = "sample2"; addItem(); //painItems.remove(getIntent().getStringExtra("position")); //adapter.notifyDataSetChanged(); } @Override // on the activityresult,get the string extra, then add the item to the list protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 1) { row1 = data.getStringExtra("com.painLogger.row1"); row2 = data.getStringExtra("com.painLogger.row2"); painLevelString = data.getStringExtra("com.painLogger.painLevel"); painLocation = data.getStringExtra("painLocation"); timeOfPainString = data.getStringExtra("com.painLogger.painTime"); textTreatmentString = data .getStringExtra("com.painLogger.treatment"); addItem(); } } // to add the item, put it in the map, and add the map into the list private void addItem() { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put("row_1", row1); map.put("row_2", row2); map.put("row_3", painLevelString); map.put("row_4", painLocation); map.put("row_5", timeOfPainString); map.put("row_6",textTreatmentString); painItems.add(map); adapter.notifyDataSetChanged(); //Null Pointer **HERE** } </code></pre> <p>Just to be clear, createNewEditedEntry() and onActivityResult() are never called at the same time and never clash. They are two completely different occurrences. It was working with just the OnActivityResult, but now when I use CreateNewEditedEntry(), it has stopped working. I have also checked and made sure that none of the Strings that I fetch from my intent are null. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload