Username and password store in shared preference

Viewed 45

This is the register fiile:

The error is when I stored data and after that, I log in when I click on the login button app got crushed. this is the register section;

 import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        EditText textview1;
        EditText textview2;
        Button button1;
        Button buttonlog;
        SharedPreferences sharedPreferences;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textview1 = (EditText)findViewById(R.id.ediuser);
            textview2 = (EditText)findViewById(R.id.edipassword);
            button1 = (Button)findViewById(R.id.btnreg);
            buttonlog = (Button)findViewById(R.id.onlogin);
    
        }
        public void onreg(View view){
            String username = textview1.getText().toString();
            String Password = textview2.getText().toString();
            sharedPreferences = getSharedPreferences("Dell", MODE_PRIVATE);
            SharedPreferences.Editor editor1 = sharedPreferences.edit();
            editor1.putString("username",username);
            editor1.putString("password",Password);
            editor1.apply();
            Toast.makeText(this, "Register Successfully", Toast.LENGTH_SHORT).show();
        }
        public void onlog(View view){
            Intent intent  = new Intent(this,login.class);
            startActivity(intent);
        }
    }
    
    this is the login file:
    package com.example.newshrd;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class login extends AppCompatActivity {
        EditText editText1;
        EditText editText2;
        Button button2;
        SharedPreferences sharedPreferences;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            editText1 = (EditText) findViewById(R.id.userlog);
            editText2 = (EditText) findViewById(R.id.passwordlog);
            button2 = (Button) findViewById(R.id.btnlog);
            sharedPreferences = getSharedPreferences("Dell", MODE_PRIVATE);
        }
       //this is the login section find error and let me know please
    
         public void onloghere(View view){
        
            String userlog = editText1.getText().toString();
            String passlog = editText2.getText().toString();
           String userlogin= sharedPreferences.getString("username",null);
            String userpaswwords=sharedPreferences.getString("passowrd",null);
        
        
        
            if (userlog.contains(userlogin) && passlog.contains(userpaswwords)){
        
        
                Toast.makeText(this, "Your are in", Toast.LENGTH_SHORT).show();
            }
            else {
        
                Toast.makeText(this, "You are not", Toast.LENGTH_SHORT).show();
        
        
            }
        }
        
        }

please, someone, find the error and let me know what I am missing in this code. It is properly formatted (at least by the preview it is), so how can I fix this? I'm new to Stack Overflow formatting.

1 Answers

When you save, key is "password":

editor1.putString("password",Password);

But when get, you have a typo for the key here "passowrd":

String userpaswwords=sharedPreferences.getString("passowrd",null);

The userpaswwords you get is null (you set SharedPreferences.getString(String key, @Nullable String defValue) defValue value to be null), and here crashes:

passlog.contains(userpaswwords)

Cuz String.contains(CharSequence s) needs the argument to be non-null.

Related