Note that there are some explanatory texts on larger screens.

plurals
  1. PONullpointerException error in android gridview
    primarykey
    data
    text
    <p>I have to develop an one android application.</p> <p>Here i have to create custom gridview adapter class.</p> <p>But am getting following error:</p> <pre><code> 06-14 14:56:26.676: E/AndroidRuntime(797): Caused by: java.lang.NullPointerException 06-14 14:56:26.676: E/AndroidRuntime(797): at com.bestinuk.adapter.CategoriesAdapter.getCount(CategoriesAdapter.java:34) 06-14 14:56:26.676: E/AndroidRuntime(797): at android.widget.GridView.setAdapter(GridView.java:180) 06-14 14:56:26.676: E/AndroidRuntime(797): at com.example.androidbestinuk.HomePage.onCreate(HomePage.java:43) </code></pre> <p>i have wrote the adapter class like below:</p> <pre><code> public class CategoriesAdapter extends BaseAdapter { private Activity activity; </code></pre> <p>List categories; private static LayoutInflater inflater=null;</p> <pre><code>public CategoriesAdapter(Activity a, List&lt;Category&gt; categories) { activity = a; this.categories=categories; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return categories.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { View vi=convertView; if(convertView==null) vi = inflater.inflate(R.layout.list_item, null); ImageView thumb_image=(ImageView)vi.findViewById(R.id.categoryimage); TextView title = (TextView)vi.findViewById(R.id.categorytitle); // title Category categorylist = (Category) getItem(position); title.setText(categorylist.getmCategoryName()); thumb_image.setImageBitmap(categorylist.getmCategoryImage()); return vi; } } </code></pre> <p>Here why am getting abovr error ???</p> <p>How can i resolve these error ???</p> <p>please give me solution for these ??? </p> <p><strong>EDIT:</strong></p> <p>yes my list is null.That's why am getting null pointer exception.</p> <p>this is my HomePage.java class:</p> <pre><code> public class HomePage extends Activity { List&lt;Category&gt; categories = new ArrayList&lt;Category&gt;();; GridView category_list; CategoriesAdapter categoryViewAdapter; private ProgressDialog pDialog; static final String URL = "http://dev.mercuryminds.com/xctesting/feed.xml"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); category_list = (GridView) findViewById(R.id.categorylist); GetXMLTask task = new GetXMLTask(this); task.execute(new String[] { URL }); } //private inner class extending AsyncTask private class GetXMLTask extends AsyncTask&lt;String, Void, Document&gt; { private Activity context; public GetXMLTask(Activity context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); // Create a progressbar pDialog = new ProgressDialog(HomePage.this); // Set progressbar title pDialog.setTitle("BestinUk"); // Set progressbar message pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); // Show progressbar pDialog.show(); } @Override protected Document doInBackground(String... urls) { Document dom = null; String xml = null; for (String url : urls) { xml = getXmlFromUrl("http://dev.mercuryminds.com/xctesting/feed.xml"); dom= XMLfunctions.XMLfromString(xml); } return dom; } @Override protected void onPostExecute(Document dom) { Category categoryBean = null; if(dom !=null) { Element root = dom.getDocumentElement(); NodeList categoryNL = root.getElementsByTagName("Category"); if(categoryNL.getLength() &gt; 0){ for (int i=0;i&lt;categoryNL.getLength();i++){ Node categoryNode = categoryNL.item(i); Element categoryElmt = null; if(categoryNode.hasAttributes()){ categoryBean = new Category(); categoryElmt = (Element)categoryNode; categoryBean.setmCategoryName(categoryElmt.getAttribute("title")); categoryBean.setmCategoryID(Integer.parseInt(categoryElmt.getAttribute("categoryid"))); categoryBean.setmCategoryImageLink(categoryElmt.getAttribute("image")); categories.add(categoryBean); categoryViewAdapter = new CategoriesAdapter(HomePage.this, categories); category_list.setAdapter(categoryViewAdapter); System.out.println("Category Title is "+ " "+categoryElmt.getAttribute("title")); System.out.println("Category Image is "+ " "+categoryElmt.getAttribute("image")); } ----------- ------------ --------- pDialog.dismiss(); } } </code></pre> <p>Now am getting following error:</p> <pre><code>06-14 16:00:21.076: E/AndroidRuntime(1305): FATAL EXCEPTION: main 06-14 16:00:21.076: E/AndroidRuntime(1305): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.androidbestinuk.Category 06-14 16:00:21.076: E/AndroidRuntime(1305): at com.bestinuk.adapter.CategoriesAdapter.getView(CategoriesAdapter.java:56) </code></pre> <p>This is my 56th line:</p> <pre><code> Category categorylist = (Category) getItem(position); </code></pre> <p>what's wrong in my code ??? please give me solution ???</p> <p>i got the output after add the below line:</p> <pre><code> Category categorylist = categories.get(position); </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.
    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