Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:56:21+00:00 2026-06-16T10:56:21+00:00

I am using sharedpreference to store the user name. When i did this inside

  • 0

I am using sharedpreference to store the user name. When i did this inside my activity it is working fine. But when i pass to a common class ( which save the values ) then it fails. Could you please guide me to the place where my code need correction ?
Thanks in advance

public class Login extends Activity  implements OnClickListener{
String userId,password,userType,key="userId";  ;
EditText etName,etPassword,etUserType;
Button go; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.login);
    initialize();
}
private void initialize(){

    etUserType=(EditText) findViewById (R.id.etUserType);   
    etName=(EditText) findViewById (R.id.etName);
    etPassword=(EditText) findViewById (R.id.etPassword);    
    go = (Button) findViewById(R.id.bGo);
    go.setOnClickListener(this); 
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub  
    userType=etUserType.getText().toString(); 
    userId=etName.getText().toString();
    password=etPassword.getText().toString();   

    if (userId.isEmpty() || password.isEmpty() ) { 
        Dialog d = new Dialog(this);
        d.setTitle("   Alert  ");
        TextView tv = new TextView(this);
        tv.setText("User Name and Password are mandatory");
        d.setContentView(tv);
        d.show();
    }else{


          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
          try {
            List<UserType> userType = PosClient.getUserTypes();
            User user = PosClient.getUser(userId, userType.get(0)); 
            LastUser lastUser = new LastUser();
            lastUser.saveUser(key, Integer.toString(user.getUserId()));  ()); 

            if  (password.equals(user.getPassword())){ 
                Intent sendBack = new Intent();
                Bundle backpack = new Bundle();
                backpack.putString("command", "proceed");  
                backpack.putSerializable("user", user);
                sendBack.putExtras(backpack);  
                setResult(RESULT_OK, sendBack);
                finish();
            }else{
                Dialog d = new Dialog(this);
                d.setTitle("   Alert  ");
                TextView tv = new TextView(this);
                tv.setText("User Name / Password is invalid");
                d.setContentView(tv);
                d.show();
            }
          } catch (ClientProtocolException e1) { 
              e1.printStackTrace();
          } catch (IOException e1) { 
              e1.printStackTrace();
          }

    }
}



public class LastUser extends Activity{
SharedPreferences file;
public static String filename = "MySharedFile";

public  String checkUser(){  
    file = getSharedPreferences(filename,0);
    String userName = file.getString("waitername", "NOID"); 
    return userName;
} 

public void saveUser(String key,String value) {  
    filename = "MySharedFile";
    file = getSharedPreferences(filename,0);
    SharedPreferences.Editor editor = file.edit();
    editor.putString(key,value);
    editor.commit();
}

}

Error details :

12-27 12:27:41.448: E/AndroidRuntime(2983): FATAL EXCEPTION: main
12-27 12:27:41.448: E/AndroidRuntime(2983): java.lang.NullPointerException
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at com.example.restaurent.LastUser.saveUser(LastUser.java:21)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at com.example.restaurent.Login.onClick(Login.java:77)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.view.View.performClick(View.java:3534)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.view.View$PerformClick.run(View.java:14263)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.os.Handler.handleCallback(Handler.java:605)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.os.Looper.loop(Looper.java:137)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at android.app.ActivityThread.main(ActivityThread.java:4441)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at java.lang.reflect.Method.invoke(Method.java:511)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 12:27:41.448: E/AndroidRuntime(2983):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:56:22+00:00Added an answer on June 16, 2026 at 10:56 am

    here

    public class LastUser extends Activity{         //<<<<<<<<
    SharedPreferences file;
    public static String filename = "MySharedFile";
      // your code here
    }
    

    you are trying to Create an instance of Activity Class for Accessing methods from other Activty which is not right way. change your code as:

    public class LastUser {
    SharedPreferences file;
    public static String filename = "MySharedFile";
    
    
    public  String checkUser(Context context){  
        file = context.getSharedPreferences(filename,0);
        String userName = file.getString("waitername", "NOID"); 
        return userName;
    } 
    
    public void saveUser(String key,String value,Context context) {  
        filename = "MySharedFile";
        file = context.getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = file.edit();
        editor.putString(key,value);
        editor.commit();
    }
    

    }

    Now for Creating SharedPreferences from Activity just pass Activity Context to methods as :

     LastUser lastUser = new LastUser();
     lastUser.saveUser(key, Integer.toString(user.getUserId()),Login.this); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SharedPreference to store the state of checkboxes but even i am
I had been using shared preferences to store some user values.... but when the
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I am using a separate class with only static fields, to store current application
I Simply want to store String name =Android using SharedPreferences from one of the
I was using SharedPreferences and everything was working fine. Then I started tweaking the
I'm using SharedPreferences in my application to store the last User id who logged
I'm writing login capability using share preference. I use SharedPreferences.Editor::commit() to store username and
I was wondering about how saving variables work by using SharedPreferences inside onStop() function.
My app has the ability to backup and restore SharedPreferences using an BackupAgentHelper class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.