从sharedpreference类信息中检索变量以放入MySQL数据库(使用Volley的Android Studio)

I need to get the username from my login info and use it as a database entry for another table (for which I have created an API) but my code only puts NULL into the database instead of the actual value.

I am able to reflect the value in a text view but it still gets saved as a NULL value in my data base. I tried using many variants of the code available online, but those do not solve my problem.

Sharedpreference class contains the following:

  public String getUsername(){

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_USERNAME, null);
}

I have created another class which is essentially a form to put data into database. In a new class (form) I am able to retrieve this using in a textview which works fine :

  textViewUsername = (TextView) findViewById(R.id.textViewUsername);
  textViewUsername.setText(SharedPrefManager.getInstance(this).getUsername());

BUT in the same class (form) I am unable to put it in a String variable (it adds NULL value to the database:

  final String username = SharedPrefManager.getInstance(this).getUsername();

In:

  params.put("usename", username);

` I am using volley to handle requests. I am using many other variables which I am retrieving from EditText in my input form, they are working just fine.

How can I retrieve the actual username and not a NULL value?

Thank you in advance.

As i am not able to comment so i post it there. can you please add a check for SharedPrefManager.getInstance(this) return null or not. If it returns null then you have to assign same instance again in your activity in oncreate or onresume method.