title: Storing username using shared preferences in android
body: <p>I am trying to save the username in shared preferences. I used a web service to get the username and password from the database and it works perfectly. The problem is with saving the username in shared preferences. I have given the code below. I have commented the place where the problem arises. I get the following exception:</p>
  
  <pre><code>   java.lang.NullPointerException
  </code></pre>
  
  <p>I cant find whats wrong. please help.</p>
  
  <p>///Login Class////</p>
  
  <pre><code>public class Login extends Activity{
  
  private static final String SOAP_ACTION = "http://tempuri.org/checkLogin";
  private static final String OPERATION_NAME = "checkLogin";
  private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
  private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
  
  Button sqllogin;
  EditText sqlusername, sqlpassword;
  TextView tvData1;
  CheckBox cb;
  
   @Override
      protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      sqlusername = (EditText) findViewById(R.id.etuname1);
      sqlpassword = (EditText) findViewById(R.id.etpass);
      tvData1 = (TextView)findViewById(R.id.textView1); 
      sqllogin = (Button) findViewById(R.id.bLogin);
  
  
      sqllogin.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
              switch (v.getId()){
              case R.id.bLogin:
              String username = sqlusername.getText().toString();
              String password = sqlpassword.getText().toString();
              SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
              Request.addProperty("uname", String.valueOf(username));
              Request.addProperty("pwd", String.valueOf(password));
  
                      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                      SoapEnvelope.VER11);
                      envelope.dotNet = true;
                      envelope.setOutputSoapObject(Request);
                      HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
  
                     try  {                    
                      httpTransport.call(SOAP_ACTION, envelope);                       
                      SoapObject result = (SoapObject) envelope.bodyIn;
                      int response =  Integer.parseInt(result.getProperty(0).toString());
                      if(response == 2)//Response is 2 when username and password valid
                          {
                          tvData1.setText("You have logged in successfully"); 
                          savePrefs("CHECKBOX",cb.isChecked()); //This line and the following 3 lines is where the problem is.
                          if (cb.isChecked())
                          {
                              savePrefs("NAME",sqlusername.getText().toString());
                          }
                          Intent openhomepage = new Intent("com.android.disasterAlertApp.HOME");
                          startActivity(openhomepage);
                          }
                      else
                          {
                          tvData1.setText("Invalid Username or password"); 
                          }
                      }  catch (Exception exception)   {
                                    tvData1.setText(exception.toString());                    
                      }
              break;
  
          }
  
          }
      });
  }
  
       private void savePrefs(String key, boolean value){
       SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
       Editor edit = sp.edit();
       edit.putBoolean(key, value);
       edit.commit();
   }
  
   private void savePrefs(String key,String value){
       SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
       Editor edit = sp.edit();
       edit.putString(key, value);
       edit.commit(); 
   }
  </code></pre>
  
  <p>}</p>