Checking if spinner is selected and having null value in Android

spinnerName is null or if getSelectedItem() returns null, calling toString() will cause your app to crash for NPE

String name= null;
if(spinnerName != null && spinnerName.getSelectedItem() !=null ) {
   name = (String)spinnerName.getSelectedItem();
} else  { 

}

if (spinner1.getCount()==0){
   Toast.makeText(getApplicationContext(),"spinner hasn't values",               
   Toast.LENGTH_LONG).show();
 }

Function for Spinner Item Selection

SpinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {             
            @Override
            public void onItemSelected(AdapterView<?> adapter, View v,int position, long id) {
                // On selecting a spinner item
                selected_item = adapter.getItemAtPosition(position).toString();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
});

Check condition for selected Item

if(selected_item.matches("")){
          //conditions accordingly  
return;
}