title: java.lang.NullPointerException- error in Android
body: <p>I'm creating a simple login activity using phone number and password for my android application. I want to pass the user entered a phone number to my welcome screen, but the problem is I'm getting the above error when the user didn't log out the first time and then after running the application. Please help me to solve this error.</p>
  
  <p>this is my LoginActivity.java file,</p>
  
  <pre><code>public class LoginActivity extends Activity {
  Button btnLogin;
  Button btnLinkToRegister;
  EditText inputphone_number;
  EditText inputPassword;
  TextView loginErrorMsg;
  
  // JSON Response node names
  private static String KEY_SUCCESS = "success";
  private static String KEY_ERROR = "error";
  private static String KEY_ERROR_MSG = "error_msg";
  private static String KEY_UID = "uid";
  private static String KEY_NAME = "name";
  private static String KEY_PHONE_NUMBER = "phone_number";
  private static String KEY_CREATED_AT = "created_at";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.login);
  
      // Importing all assets like buttons, text fields
      inputphone_number = (EditText) findViewById(R.id.loginEmail);
      inputPassword = (EditText) findViewById(R.id.loginPassword);
      btnLogin = (Button) findViewById(R.id.btnLogin);
      btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
      loginErrorMsg = (TextView) findViewById(R.id.login_error);
  
      // Login button Click Event
      btnLogin.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View view) {
              String phone_number = inputphone_number.getText().toString();
              String password = inputPassword.getText().toString();
              UserFunctions userFunction = new UserFunctions();
              Log.d("Button", "Login");
              JSONObject json = userFunction.loginUser(phone_number, password);
  
              // check for login response
              try {
                  if (json.getString(KEY_SUCCESS) != null) {
                      loginErrorMsg.setText("");
                      String res = json.getString(KEY_SUCCESS); 
                      if(Integer.parseInt(res) == 1){
                          // user successfully logged in
                          // Store user details in SQLite Database
                          DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                          JSONObject json_user = json.getJSONObject("user");
  
                          // Clear all previous data in database
                          userFunction.logoutUser(getApplicationContext());
                          db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_PHONE_NUMBER), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                     
  
                          // Launch Dashboard Screen
                          Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
  
                          // this is how i sent phone number to next activity
                          dashboard.putExtra("phone_number", phone_number);
  
  
                          // Close all views before launching Dashboard
                          dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                          startActivity(dashboard);
  
                          // Close Login Screen
                          finish();
                      }else{
                          // Error in login
                          loginErrorMsg.setText("Incorrect username/password");
                      }
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      });
  
      // Link to Register Screen
      btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View view) {
              Intent i = new Intent(getApplicationContext(),
                      RegisterActivity.class);
              startActivity(i);
              finish();
          }
      });
  }
  }
  </code></pre>
  
  <p>this is MainActivity.java file,</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  UserFunctions userFunctions;
  Button btnLogout;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  
         /**
       * Dashboard Screen for the application
       * */        
      // Check login status in database
      userFunctions = new UserFunctions();
      if(userFunctions.isUserLoggedIn(getApplicationContext())){
          setContentView(R.layout.dashboard);
  
          TextView phone_number = (TextView) findViewById(R.id.userNameView);
  
          Intent i = getIntent();
  
          String phoneNumber = i.getExtras().getString("phone_number");
          phone_number.setText(phoneNumber);
  
          btnLogout = (Button) findViewById(R.id.btnLogout);
  
          btnLogout.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  userFunctions.logoutUser(getApplicationContext());
                  Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                  login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(login);
                  // Closing dashboard screen
                  finish();
              }
          });
  
      }else{
          // user is not logged in show login screen
          Intent login = new Intent(getApplicationContext(), LoginActivity.class);
          login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(login);
          // Closing dashboard screen
          finish();
      }
  //  setContentView(R.layout.main);
  
  //  MainDBAdapter mainDBAdapter = new MainDBAdapter(this);
      DatabaseHandler mainDBHandler = new DatabaseHandler(this);
  
  
  }
  
  public void onClickedJobs(View view){
  
      Intent intent = new Intent(this,TourNumberList.class);
      startActivity(intent);
  
  }
   }
  </code></pre>
  
  <p>UserFunctions.java file,</p>
  
  <pre><code>public class UserFunctions {
  
  private JSONParser jsonParser;
  
  private static String loginURL = "http://10.0.2.2/android_login_api/";
  private static String registerURL = "http://10.0.2.2/android_login_api/";
  
  private static String login_tag = "login";
  private static String register_tag = "register";
  
  // constructor
  public UserFunctions(){
      jsonParser = new JSONParser();
  }
  
  /**
   * function make Login Request
   * @param phone_number
   * @param password
   * */
  public JSONObject loginUser(String phone_number, String password){
      // Building Parameters
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", login_tag));
      params.add(new BasicNameValuePair("phone_number", phone_number));
      params.add(new BasicNameValuePair("password", password));
      JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
      // return json
      // Log.e("JSON", json.toString());
      return json;
  }
  
  /**
   * function make Login Request
   * @param name
   * @param phone_number
   * @param password
   * */
  public JSONObject registerUser(String name, String phone_number, String password){
      // Building Parameters
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", register_tag));
      params.add(new BasicNameValuePair("name", name));
      params.add(new BasicNameValuePair("phone_number", phone_number));
      params.add(new BasicNameValuePair("password", password));
  
      // getting JSON Object
      JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
      // return json
      return json;
  }
  
  /**
   * Function get Login status
   * */
  public boolean isUserLoggedIn(Context context){
      DatabaseHandler db = new DatabaseHandler(context);
      int count = db.getRowCount();
      if(count &gt; 0){
          // user logged in
          return true;
      }
      return false;
  }
  
  /**
   * Function to logout user
   * Reset Database
   * */
  public boolean logoutUser(Context context){
      DatabaseHandler db = new DatabaseHandler(context);
      db.resetTables();
      return true;
  }
  
  }
  </code></pre>
  
  <p>This is LogCat error,</p>
  
  <pre><code>04-08 10:27:56.763: E/AndroidRuntime(342): FATAL EXCEPTION: main
  04-08 10:27:56.763: E/AndroidRuntime(342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourinfo/com.example.tourinfo.MainActivity}: java.lang.NullPointerException
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:99)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:507)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at dalvik.system.NativeStart.main(Native Method)
  04-08 10:27:56.763: E/AndroidRuntime(342): Caused by: java.lang.NullPointerException
  04-08 10:27:56.763: E/AndroidRuntime(342):  at com.example.tourinfo.MainActivity.onCreate(MainActivity.java:37)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  04-08 10:27:56.763: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  04-08 10:27:56.763: E/AndroidRuntime(342):  ... 11 more
  </code></pre>