Question

I'm a new Android programmer and recently, I am parsing xml file using pull parser in list view. when parsing in listview i am getting this exception.

09-13 05:37:00.401: E/AndroidRuntime(1167): FATAL EXCEPTION: main
09-13 05:37:00.401: E/AndroidRuntime(1167): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplexmlpullapp/com.example.simplexmlpullapp.MainActivity}: java.lang.NullPointerException

So what to do next ? so that this exception will get removed. my code is:

MainActivity.java

 package com.example.simplexmlpullapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ListView list;
    String str;
    ArrayList<Data> arrdata;
    ArrayList<String> arrayofString, parcedCountry;
    String textforinput = "<foo>Hello World!</foo>";
    String parsedData = "";
    XmlPullParser xmlPullParser;
    ArrayAdapter<String>adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView)findViewById(R.id.li);

        adapter=new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1,android.R.id.text1,arrayofString);
        list.setAdapter(adapter);
         try
         {
                 InputStream is;
                 is=getAssets().open("countries.xml");
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 StringBuilder total = new StringBuilder();
                 String line;
                 while ((line = r.readLine()) != null)
                 {
                         total.append(line);
                 }

                 //Log.e("File",""+total.toString()); 
                 parseCountries(total.toString());
         }
         catch (Exception e) 
         {
                 // TODO: handle exception
         }
}

private void parseCountries(String string)
{

        int eventType=0;
        String countries = null;
        boolean countryFlag=false;
        ArrayList<String>parsedCountry=new ArrayList<String>();
        try 
        {
                xmlPullParser=XmlPullParserFactory.newInstance().newPullParser();
                xmlPullParser.setInput(new StringReader(string));
                eventType = xmlPullParser.getEventType();
        }
        catch (XmlPullParserException e) {
                e.printStackTrace();
        }

        while(eventType!=XmlPullParser.END_DOCUMENT)
        {
                switch (eventType) 
                {
                case XmlPullParser.START_DOCUMENT:
                        break;

                case XmlPullParser.START_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {

                                countryFlag=true;        
                        }
                        break;
                case XmlPullParser.TEXT:

                        if(countryFlag)
                        {
                                countries=xmlPullParser.getText().toString().trim();
                                Log.i("parse_example", "country"+countries); 
                        }        
                        break;

                case XmlPullParser.END_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {
                                parsedCountry.add(countries);
                                countryFlag=false;        
                        }
                        break;
                        //end of switch        
                }

                try {
                    eventType=xmlPullParser.next();

                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }

//        arrayofString=new ArrayList<String>();
//        for(int i=0;i<arrdata.size();i++)
//        {
//            Data data=arrdata.get(i);
//            arrayofString.add(data.getCountry());
//            Log.i("parse_example", "country"+countries);
//        }

}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lodge Names"
        tools:context=".MainActivity" />

    <ListView
        android:id="@+id/li"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000" >
    </ListView>

</LinearLayout>
Was it helpful?

Solution

Please do this in onCreate();

initialize before creating the adapter

arrayofString = new ArrayList<String>();
parcedCountry = new ArrayList<String>();

UPDATE to show list

adapter=new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1,android.R.id.text1,parcedCountry); //UPDATE HERE
        list.setAdapter(adapter);
         try
         {
                 InputStream is;
                 is=getAssets().open("countries.xml");
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 StringBuilder total = new StringBuilder();
                 String line;
                 while ((line = r.readLine()) != null)
                 {
                         total.append(line);
                 }

                 //Log.e("File",""+total.toString()); 

                 parseCountries(total.toString());

                 adapter.notifyDataSetChanged(); //UPDATE HERE

         }
         catch (Exception e) 
         {
                 // TODO: handle exception
         }

OTHER TIPS

try this..

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.li);
            arrayofString = new ArrayList<String>();
            parcedCountry = new ArrayList<String>();
    try {
        InputStream is;
        is = getAssets().open("countries.xml");
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }

        // Log.e("File",""+total.toString());
        parseCountries(total.toString());
        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                arrayofString);
        list.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top