Update and remove GridView items at runtime <p>i am developing a project where i compare the images of two items,So if two items will have same image after clicking these items should be hide. my code is given below and this code encounter a problem. is this a logical error or any other issue? i try to solve the issue but did't resolve.. Please guide me... here is my main Activity.<br> MainActivity.java </p> <pre><code>public class MainActivity extends Activity { Context ctx; int imagesArray[]; ImageAdapter adapter; List&lt;Integer&gt; pictures; boolean flage = false; GridView gridView; int save1, save2; int img1 = -1, img2 = -1; public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView myimage = new ImageView(ctx); gridView = (GridView) findViewById(R.id.gv_memory); gridView.setAdapter(adapter); shuffleArray(); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub myimage.setImageResource(pictures.get(position)); if (flage == false) { img1 = pictures.get(position); flage = true; } else if (flage == true) { img2 = pictures.get(position); checkResult(); flage = false; } } private void checkResult() { // TODO Auto-generated method stub if (img1 == img2) { adapter.pictureList.remove(img1); adapter.pictureList.remove(img2); adapter.notifyDataSetChanged(); Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show(); } } }); } private void shuffleArray() { // TODO Auto-generated method stub pictures = new ArrayList&lt;Integer&gt;(); for (int index = 0; index &lt; OriginalArray.length; index++) { pictures.add(OriginalArray[index]); } Collections.shuffle(pictures); } } </code></pre> <p>ImageAdapter.java </p> <pre><code>public class ImageAdapter extends BaseAdapter { private Context context; List&lt;Integer&gt; pictureList = new ArrayList&lt;Integer&gt;(); public ImageAdapter(Context c) { context = c; for (int i = 0; i &lt; 8; i++) { pictureList.add(R.drawable.question); } } @Override public int getCount() { // TODO Auto-generated method stub return (pictureList.size()); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return pictureList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView myimage = new ImageView(context); myimage.setImageResource(pictureList.get(position)); myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE); myimage.setLayoutParams(new GridView.LayoutParams(70, 70)); return myimage; } } </code></pre> <p>LogCat. </p> <pre><code>03-14 06:09:03.793: D/dalvikvm(2877): GC_FOR_ALLOC freed 54K, 8% free 2771K/2996K, paused 111ms, total 117ms 03-14 06:09:03.802: I/dalvikvm-heap(2877): Grow heap (frag case) to 3.943MB for 1127536-byte allocation 03-14 06:09:03.922: D/dalvikvm(2877): GC_FOR_ALLOC freed 2K, 6% free 3870K/4100K, paused 118ms, total 118ms 03-14 06:09:03.962: D/AndroidRuntime(2877): Shutting down VM 03-14 06:09:03.962: W/dalvikvm(2877): threadid=1: thread exiting with uncaught exception (group=0x41465700) 03-14 06:09:03.972: E/AndroidRuntime(2877): FATAL EXCEPTION: main 03-14 06:09:03.972: E/AndroidRuntime(2877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memory/com.example.memory.MainActivity}: java.lang.NullPointerException 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Handler.dispatchMessage(Handler.java:99) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Looper.loop(Looper.java:137) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.main(ActivityThread.java:5103) 03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invokeNative(Native Method) 03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invoke(Method.java:525) 03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 03-14 06:09:03.972: E/AndroidRuntime(2877): at dalvik.system.NativeStart.main(Native Method) 03-14 06:09:03.972: E/AndroidRuntime(2877): Caused by: java.lang.NullPointerException 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.ViewConfiguration.get(ViewConfiguration.java:318) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.View.&lt;init&gt;(View.java:3264) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.widget.ImageView.&lt;init&gt;(ImageView.java:112) 03-14 06:09:03.972: E/AndroidRuntime(2877): at com.example.memory.MainActivity.onCreate(MainActivity.java:33) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Activity.performCreate(Activity.java:5133) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 03-14 06:09:03.972: E/AndroidRuntime(2877): ... 11 more 03-14 06:14:04.503: I/Process(2877): Sending signal. PID: 2877 SIG: 9 </code></pre>