1

When ever im calling fromcheckpass(mcontext) im getting nullpointerexception. Where imdoing wrong. Help me out!

From onClick() i'm calling fromcheckpass(mcontext).

public void onClick(View v) {
    InboxActivity inboxActivity = new InboxActivity();
    inboxActivity.fromcheckpass(CheckPass.this);
}

How can I call fromcheckpass(mcontext) method ?

public void fromcheckpass(Context mcontext) {
    Toast.makeText(mcontext, "WEL COME", Toast.LENGTH_LONG).show();
    Db = new MySQLiteHelper(this);
    String DbInsert = Utils.getPreferences("DbInsert", this);

    if (!DbInsert.equalsIgnoreCase("Inserted")) {
        SaveDataInDB();
    }

    MessageListView = (ListView) findViewById(R.id.lvInbox);
    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new    ColorDrawable(Color.parseColor("#0154A4")));
    bar.setTitle(R.string.app_name);
    bar.setTitle(Html.fromHtml("<font color='#ffffff'>WeText </font>"));
    bar.setIcon(R.drawable.icon_top);

    Utils.getOverflowMenu(this);

    attachListeners();

    dataList = Utils.getLatestMessageOfAllContacts(InboxActivity.this);

    if (dataList.isEmpty()) {
        Toast.makeText(getApplicationContext(), "NO MESSAGES", Toast.LENGTH_LONG).show();
    } else {
        iAdapter = new Adapter(InboxActivity.this, dataList);
        MessageListView.setAdapter(iAdapter);
    }
}

Whenever I call fromCheckpass(mcontex) method the app crashes. Here is my logcat:

Process: com.futureappspk.WeTextFree, PID: 29065
    java.lang.NullPointerException
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:186)
        at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
        at com.futureappspk.WeTextFree.Utils.getPreferences(Utils.java:432)
        at com.futureappspk.WeTextFree.InboxActivity.fromcheckpass(InboxActivity.java:130)
        at com.futureappspk.WeTextFree.CheckPass$1.onClick(CheckPass.java:40)

Here is my util class method which is calling fromcheckpass(mcontext)

public static String getPreferences(String key, Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String userName = sharedPreferences.getString(key, "UserName");
    return userName;
}
Narendra Baratam
  • 828
  • 1
  • 12
  • 26
robinHood013
  • 209
  • 1
  • 2
  • 15

2 Answers2

0

Please try This
I think your CheckPass is not your Activity.
Hence pass Context correctly like
getActivity() or getApplicationContext();
in onClick Method Hope it helps

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
0

To inform you the error is at this line

String DbInsert = Utils.getPreferences("DbInsert", this);

Possible reasons

You have to use getSharedPreferences with the instance of Context class like

ctx.getSharedPreferences("MY_PREF",0);

You have not shown your Utils class but as I guess getPreferences method is a static method of the class and you are calling it directly without initializing any Context of application. So I suggest you to initialize the Context in the Constructor of your Utils class and use the getSharedPreferences method only with the instance of Utils class.

UPDATE

Try not to make it static, do something like this

public class Utils {
private SharedPreferences sharedPreferences; 

public Utils (Context context) {
   this.sharedPreferences = getPreferences(MODE_PRIVATE);    
}
public String getPreferences(String key) {
   sharedPreferences.getString(key, "UserName");
    return userName;
  }
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68