0

I trying to use List View so show my date but i am getting exception .When i am running my application is is throwing the run time exception.Please suggest me what i have to do to get away from this execption : This is my Exception

08-06 12:17:09.278: E/AndroidRuntime(3709): FATAL EXCEPTION: main
08-06 12:17:09.278: E/AndroidRuntime(3709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridtestproject/com.example.gridtestproject.TestActivity}: java.lang.NullPointerException
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.os.Looper.loop(Looper.java:137)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at java.lang.reflect.Method.invoke(Method.java:511)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at dalvik.system.NativeStart.main(Native Method)
08-06 12:17:09.278: E/AndroidRuntime(3709): Caused by: java.lang.NullPointerException
08-06 12:17:09.278: E/AndroidRuntime(3709):     at com.example.gridtestproject.TestActivity.onCreate(TestActivity.java:47)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.Activity.performCreate(Activity.java:5104)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-06 12:17:09.278: E/AndroidRuntime(3709):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-06 12:17:09.278: E/AndroidRuntime(3709):     ... 11 more

Here is main class file

public class TestActivity extends Activity {

    ListView onewayListView,ReturnListView;
    OneWayFlightResult onewaydata;
    ReturnFlightResult returndata;
    static final String FlightNumber = ""; // parent node
    static final String FlightCompanyName = "";
    static final String FlightTime = "";
    static final String FlightStop ="";
    static final String FlightCost ="";

/** Called when the activity is first created. */

@Override
protected void onCreate(Bundle savedInstanceState) {
    ArrayList<HashMap<String, String>> flightData = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 12; i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
        map.put(FlightCompanyName, "SpiceJet");
        map.put(FlightNumber, "SG-123");
        map.put(FlightTime, "6:00 - 7:00");
        map.put(FlightStop, "1h 30m | Non Stop");
        map.put(FlightCost, "Rs 20,000");

        // adding HashList to ArrayList
        flightData.add(map);
    }


    onewayListView=(ListView)findViewById(R.id.lvDepartures);
    ReturnListView=(ListView)findViewById(R.id.lvArrivals);

    // Getting adapter by passing xml data ArrayList
    onewaydata=new OneWayFlightResult(this, flightData);        
    onewayListView.setAdapter(onewaydata);

   // returndata=new ReturnFlightResult(this, flightData);        
   // onewayListView.setAdapter(returndata);
    }
}

This is my OneWayResult Class Code

public class OneWayFlightResult extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;


    public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
         return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.row, null);
        TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
        TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
        TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
        ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);

        HashMap<String, String> flight = new HashMap<String, String>();
        flight = data.get(position);

        flightTime.setText(flight.get(TestActivity.FlightTime));
        flightCompanyName.setText(TestActivity.FlightCompanyName);
        flightNumber.setText(TestActivity.FlightNumber);
        return vi;
    }
}
2
  • 1
    What is there in Line Number : 47 ? Commented Aug 6, 2013 at 12:24
  • Most probably one of your findViewById is failing. Try wrapping those in a try catch and log the error, should give you a better idea about the cause of the error. Commented Aug 6, 2013 at 12:27

1 Answer 1

5

You are missing setContentView.

  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState); // missing
  setContentView(R.layout.mylayout); // missing
      ...// rest of the code
  }

And you are trying to initialize your listview

      onewayListView=(ListView)findViewById(R.id.lvDepartures);

You have not set the content to the activity. So if you try to initialize you will get NullPointerException.

You can findViewById of the current view hierarchy set to the activity.

Sign up to request clarification or add additional context in comments.

12 Comments

Nice.. And very quick answer. +1
@Hardy happen to notice it and glad i could help.
@Raghunandan as u have suggested me to use listView i have don the same but i am facing some issue i need u r help to complete this
@Gaurav Can we know what is there at line 47 ?
@Hardy see what raghu have mentioned that was the issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.