java.lang.String java.lang.Object.toString()' on a null object reference
<p>I am developing an app with mysql database,here I'm using spinner and I want to store spinner selected item into database.I am doing following code but when I am running the app it get crashed and shows the error in the logcat i.e </p>

<p>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.user.spinnerdemo.MainActivity_D6$1.onClick(MainActivity_D6.java:100)<br>
please give me a suggestion what can i do here.</p>

<pre><code>//java file
public class MainActivity_D6  extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    private Spinner spinner2, spinner1;
    private String  s_name, s_course;
    //An ArrayList for Spinner Items

    private ArrayList&lt;String&gt; students1;
    private ArrayList&lt;String&gt; students2;

    Button mBtnSave;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    //JSON Array

    private JSONArray result1, result2, result;

    //TextViews to display details
    private TextView textViewName1;
    private TextView textViewName2;
    private TextView textViewCourse;
    private TextView textViewSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity_d);

        //Initializing the ArrayList
        students1 = new ArrayList&lt;String&gt;();
        students2 = new ArrayList&lt;String&gt;();

        //Initializing Spinner


        //Adding an Item Selected Listener to our Spinner
        //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener


        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        spinner1.setOnItemSelectedListener(this);
        spinner2.setOnItemSelectedListener(this);

        mBtnSave = (Button) findViewById(R.id.button2);

        mBtnSave.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {

        String result = null;
        InputStream is = null;

        ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();

                s_name = spinner2.getSelectedItem().toString();
                s_course = spinner1.getSelectedItem().toString();

                nameValuePairs.add(new BasicNameValuePair("s_course",s_course));
                nameValuePairs.add(new BasicNameValuePair("s_name",s_name));


        StrictMode.setThreadPolicy(policy);


        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://example.in/Spinner/insert_s2.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.e("log_tag", "connection success ");
            Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }


        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getApplicationContext(), "Connection fail",  Toast.LENGTH_SHORT).show();

        }
        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
                Intent i = new Intent(getBaseContext(),DatabaseActivity.class);
                startActivity(i);
            }
            is.close();

            result=sb.toString();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error converting result "+e.toString());
        }


        try{

            JSONObject json_data = new JSONObject(result);

            CharSequence w= (CharSequence) json_data.get("re");

            Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();


        }
        catch(JSONException e)
        {
            Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
        }



    }
});

   /*     mBtnSave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

               // submitForm();

            }
        });*/

        //Initializing TextViews
        textViewName1 = (TextView) findViewById(R.id.textViewName1);
        textViewName2 = (TextView) findViewById(R.id.textViewName2);

        //This method will fetch the data from the URL
        getData1();
        getData2();

    }

 /*   private void submitForm() {

        s_name = spinner2.getSelectedItem().toString();
        //     s_name="aaa";
        s_course = spinner1.getSelectedItem().toString();
        //      s_course="bbb";
        Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
        new InsertActivity(this).execute(s_course, s_name);


    }*/

    @Override
    public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {

    /*    switch (parent.getId()){

            case R.id.spinner1:
               s_course=  parent.getItemAtPosition(position).toString();
                //    getData1();
                // showToast("Spinner1: position=" + position);

                break;


            case R.id.spinner2:
               s_name=  parent.getItemAtPosition(position).toString();
                //    getData2();
                break;
        }*/
    }

    @Override
    public void onNothingSelected(AdapterView&lt;?&gt; parent) {


    }

/*    private String getName(int position){
        String name="";
        try {
            //Getting object of given index
            JSONObject json = result.getJSONObject(position);

            //Fetching name from that object
            name = json.getString(Config.TAG_NAME);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Returning the name
        return name;
    }
    private String getCourse(int position){
        String course="";
        try {
            JSONObject json = result.getJSONObject(position);
            course = json.getString(Config.TAG_COURSE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return course;
    }*/

    private void getData1() {
        //Creating a string request
        StringRequest stringRequest1 = new StringRequest(Config.DATA_URL1,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response1) {
                        JSONObject j1 = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j1 = new JSONObject(response1);

                            //Storing the Array of JSON String to our JSON Array
                            result1 = j1.getJSONArray(Config.JSON_ARRAY1);

                            //Calling method getStudents to get the students from the JSON Array
                            getStudents1(result1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error1) {

                    }
                });

        //Creating a request queue
        RequestQueue requestQueue1 = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue1.add(stringRequest1);
    }

    private void getStudents1(JSONArray j1) {
        //Traversing through all the items in the json array
        for (int i = 0; i &lt; j1.length(); i++) {
            try {
                //Getting json object
                JSONObject json1 = j1.getJSONObject(i);

                //Adding the name of the student to array list
                students1.add(json1.getString(Config.TAG_COURSE));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //Setting adapter to show the items in the spinner
        spinner1.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity_D6.this, android.R.layout.simple_spinner_dropdown_item, students1));
    }



    private void getData2() {
        //Creating a string request
        StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response2) {
                        JSONObject j2 = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j2 = new JSONObject(response2);

                            //Storing the Array of JSON String to our JSON Array
                            result = j2.getJSONArray(Config.JSON_ARRAY);

                            //Calling method getStudents to get the students from the JSON Array
                            getStudents2(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error1) {

                    }
                });

        //Creating a request queue
        RequestQueue requestQueue2 = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue2.add(stringRequest2);
    }

    private void getStudents2(JSONArray j2) {
        //Traversing through all the items in the json array
        for (int i = 0; i &lt; j2.length(); i++) {
            try {
                //Getting json object
                JSONObject json2 = j2.getJSONObject(i);

                //Adding the name of the student to array list
                students2.add(json2.getString(Config.TAG_USERNAME));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //Setting adapter to show the items in the spinner
        spinner2.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity_D6.this, android.R.layout.simple_spinner_dropdown_item, students2));
    }

}
</code></pre>