```
package com.android.housingapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.housingapp.helper.JSONParser;
import com.android.housingapp.helper.ListViewAdapter;
import com.android.housingapp.helper.ListViewAdapterComplaints;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class BillsandDuesSpinner extends Activity{
Button btnaddyear,btnnext;
String name_array[];
Spinner yearspinner, spmonth;
String year_id,year,month,own_id;
String url;
JSONParser jParser ;
JSONArray montharray= null;
Context context=this;
int success=0;
JSONObject json;
ArrayList<HashMap<String, String>> monthList;
List<NameValuePair> params = new ArrayList<NameValuePair>();
ProgressDialog pDialog;
private static final String TAG_SUCCESS = "success";
private static final String TAG_DUE_MAIN= "all_year_list";
private static final String TAG_DUE_ID = "year_id";
private static final String TAG_DUE_MON = "month";
private static final String TAG_DUE_YEAR = "year";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.billsandduesspinner);
        btnaddyear=(Button)findViewById(R.id.btnaddyear);
        btnnext=(Button)findViewById(R.id.btnnext);
        yearspinner=(Spinner)findViewById(R.id.spyear);
        spmonth=(Spinner)findViewById(R.id.spmonth);
        own_id=getIntent().getExtras().getString("own_id");
        monthList =new ArrayList<HashMap<String, String>>();
        new BindYear().execute();

        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.month,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spmonth.setAdapter(adapter);
        url="http://10.0.2.2:80/Android/HousingApp/get_due_mon_spinner.php";
        btnaddyear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               Intent i=new Intent(getApplicationContext(),NewYear.class);
               startActivity(i);
            }
        });

        btnnext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i=new Intent(BillsandDuesSpinner.this,TypesofBills.class);
            startActivity(i);

            }
        });

    }

    class BindYear extends AsyncTask<String,String,String>{

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(BillsandDuesSpinner.this);
            pDialog.setMessage("Loading complains. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }       

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            // contactList = new ArrayList<HashMap<String, String>>()
                // Creating JSON Parser instance
                 jParser = new JSONParser();

                // getting JSON string from URL

                try {
                    json = jParser.getJSONFromUrl(url); 
                    if(json.has(TAG_DUE_MAIN))
                    {
                    // Getting Array of Contacts
                    montharray = json.getJSONArray(TAG_DUE_MAIN);
                    name_array = new String[montharray.length()];

                    // looping through All Contacts
                    for(int i = 0; i < montharray.length(); i++){
                        JSONObject c = montharray.getJSONObject(i);

                        // Storing each json item in variable
                      year_id=c.getString(TAG_DUE_ID);
                      month = c.getString(TAG_DUE_MON);
                      year = c.getString(TAG_DUE_YEAR);
                      name_array[i] = c.getString(TAG_DUE_YEAR);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_DUE_ID,year_id );
                        map.put(TAG_DUE_MON,month);
                        map.put(TAG_DUE_YEAR,year );

                        // adding HashList to ArrayList
                        monthList.add(map);
                    }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            return null;
        }

        protected void onPostExecute(String file_url) {         

            // Pass the results into ListViewAdapter.java
            try{
            if(json.has(TAG_DUE_MAIN))
            {

            ArrayAdapter<String> adapter =  new ArrayAdapter<String> (BillsandDuesSpinner.this, android.R.layout.simple_spinner_item, name_array);       
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            yearspinner.setAdapter(adapter);

            }
            else{
                Toast.makeText(BillsandDuesSpinner.this,"No year is available now.Please add one.",Toast.LENGTH_SHORT).show();
            }
            }catch(Exception e)
            {
                e.printStackTrace();
            }

            pDialog.dismiss();
        }

    }

}

```

Here is a simple code of binding json data in a spinner.My php code for fetching json data is absolutely alright.The problem is whenever I try to bind json data in the spinner,at first the app crashes(showing null pointer exception) or it bind nothing into the spinner.But when I again click the button which will bind the json data into the spinner (it resides into another activity), **after crashing(not running the app again)**,then the data is binded to the spinner sucessfully.So what is the problem??why the data is not binding at the first time??