Note that there are some explanatory texts on larger screens.

plurals
  1. POApplication throws null pointer exception when we try to capture selected checkbox ids on submit button in Android
    primarykey
    data
    text
    <p>I have a grid layout with a EditText field on the top of the screen. I am implementing base adapter to fill in the value for grid layout. </p> <p>The code I am using is as follows. My main activity </p> <pre><code>public class MainActivityDynamic extends Activity{ GridView gridView; GridObject[] planets; ArrayList&lt;GridObject&gt; myObjects; Button Go; ImageAdapter adapter; int position; boolean[] itemChecked; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.gridlayout); Go = (Button)findViewById(R.id.bGO); gridView = (GridView) findViewById(R.id.gV); String[] primarycontext = {"first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","leventh","twelth","thirteenth","fouteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twenteth","twentyfirst","twentysecond", "twentythird","twentythird","twentyfouth"}; gridView.setAdapter(new ImageAdapter(this, primarycontext)); gridView.setOnItemClickListener(new GridView.OnItemClickListener(){ public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position, long id) { // TODO Auto-generated method stub //myObjects.get(position).setState(1); //myAdapter.notifyDataSetChanged(); Toast.makeText(getBaseContext(), "clicked on "+position, Toast.LENGTH_SHORT).show(); } }); Go.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //adapter.notifyDataSetChanged(); int len = 10; int cnt = 0; String selectImages = ""; for (int i = 0;i&lt;len; i++){ if (adapter.itemChecked[i]){ cnt++; } } if(cnt == 0){ Toast.makeText(getBaseContext(), "please select atleast one", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getBaseContext(), "you have selected" + cnt + "boxes", Toast.LENGTH_SHORT).show(); } String item = (String) adapter.getItem(position); Toast.makeText(getBaseContext(), "selected fields"+item, Toast.LENGTH_SHORT).show(); } }); } } public class ImageAdapter extends BaseAdapter { Context mContext; public ImageAdapter(Context c,String[] primarycontext) { mContext = c; this.primarycontext = primarycontext; } @Override public int getCount() { // TODO Auto-generated method stub return providers.length; } public Object getItem(int position) { // TODO Auto-generated method stub return primarycontext[position]; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v; if(convertView==null){ LayoutInflater li = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = new View(mContext); v = li.inflate(R.layout.icon, null); } else { v = convertView; } ImageView iv = (ImageView)v.findViewById(R.id.grid_item_image); iv.setImageResource(R.drawable.folder_0); CheckBox CBx = (CheckBox)v.findViewById(R.id.cBIon); CBx.setText(primarycontext[position]); CBx.setChecked(isItemChecked(position)); return v; } private boolean isItemChecked(int position) { // TODO Auto-generated method stub System.out.println("the returning items="+itemChecked); return itemChecked; } } } </code></pre> <p>In the main layout I have a gridview occupied with a checkboxes with text and an Image. I have a Submit(Go) button which when clicked should capture all names of the checked boxes checked in main activity. How can I do that? When ever I run this code and click on Go, the application crashes and returns null pointer exception.</p> <p>Errors</p> <pre><code>11-25 05:43:50.390: E/AndroidRuntime(519): FATAL EXCEPTION: main 11-25 05:43:50.390: E/AndroidRuntime(519): java.lang.NullPointerException 11-25 05:43:50.390: E/AndroidRuntime(519): at com.example.kam1.MainActivityDynamic$2.onClick(MainActivityDynamic.java:73) 11-25 05:43:50.390: E/AndroidRuntime(519): at android.view.View.performClick(View.java:2408) 11-25 05:43:50.390: E/AndroidRuntime(519): at android.view.View$PerformClick.run(View.java:8816) 11-25 05:43:50.390: E/AndroidRuntime(519): at android.os.Handler.handleCallback(Handler.java:587) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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