Skip to content Skip to sidebar Skip to footer

Android Listview Nullpointerexception

I am having problems populating the ListView. Logcat Shows NullPointerExcepton. I have also posted the code and logcat below. The code was did in a hurry and I am just a beginner.

Solution 1:

Here i have modified some quick code ..

there it may generate an error if you tried to load the system contain View in some background thread

publicclassUnicalNotificationsActivityextendsListActivity {
    String notificationlinks[] = newString[100];
    String notificationnames[] = newString[100];
    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Create a list view 
        ArrayAdapter<String> adapter=newArrayAdapter<String>(UnicalNotificationsActivity.this,R.layout.exams,notificationnames);
        setListAdapter(adapter);
        ListViewlv= getListView();

        //Set item click listener
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(newOnItemClickListener() {

            @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                //When an item is clicked, send the corresponding link to getResults classIntenti=newIntent(getApplicationContext(), getResults.class);       
                i.putExtra("examlink", notificationlinks[(int)arg3]);
                startActivity(i);

            }

        });

        newDownloadNotificationTask().execute();
    }


    /////////////////////////privateclassDownloadNotificationTaskextendsAsyncTask<String, Void, String> {
        @OverrideprotectedvoidonPreExecute(){
            Toast.makeText(UnicalNotificationsActivity.this ,"Loading", Toast.LENGTH_LONG);
        }
        @OverrideprotectedvoidonPostExecute(String result) {


        }
        @Overrideprotected String doInBackground(String... arg0) {
            URLuni=null;
            try {
                uni = newURL("http://universityofcalicut.info/index.php?option=com_content&task=view&id=744&Itemid=324");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            String url=uni.toString();
            Documentdoc=null;

            try {
                doc = Jsoup.connect(url).get();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Elementslinks= doc.select("a[href]");
            int index=-1;
            int i=0;

            //Iterate every link and select only those with the string containing 202.88.252.6for (Element link : links) {
                index=-1;
                index=link.attr("abs:href").indexOf(".pdf");

                if(index!=-1 && link.attr("abs:href")!=null && link.text()!=null && link.text()!=" " && i<100)
                {
                    //Store the link and corresponding names
                    notificationlinks[i]=link.attr("abs:href");
                    notificationnames[i]=link.text();

                    Log.d("PDF FOUND", notificationlinks[i]);
                }
            }
            returnnull;
        }
    }
    ///////////////
}

Solution 2:

I think this is not the correct approach.. because you are using an async task... which runs in different thread... and by the time it returns the result... the oncreate is executed...and the adapter is empty as async task has not returned any result yet... so it is giving you null pointer exception...

Post a Comment for "Android Listview Nullpointerexception"