getting "app stoped working when opening app" if i use shared preferences in a simple #, here i ll share the code. #android

+1 Yellow flash · February 7, 2016
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference

                                                     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:171)


mainActivity.java
package com.example.me.rateme;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Set;

public class Rater extends Activity {

EditText ed1,ed2,ed3;
Set<String> myl1;
Set<String> myl2;
Set<String> myl3;
private static final String Myshared="mypreferance";
SharedPreferences shared =this.getSharedPreferences(Myshared, Context.MODE_PRIVATE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rater);
ed1=(EditText)findViewById(R.id.Editview1);
ed2=(EditText)findViewById(R.id.Editview2);
ed3=(EditText)findViewById(R.id.Editview2);
}
public void save(View v) {
myl1.add(ed1.getText().toString());
myl2.add(ed2.getText().toString());
myl3.add(ed3.getText().toString());
SharedPreferences.Editor edit = shared.edit();
edit.putStringSet("name", myl1);
edit.putStringSet("rate", myl2);
edit.putStringSet("comment", myl3);
edit.apply();
Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
}
public void show(View v) {
Intent in = new Intent(this, Show.class);
startActivity(in);
}

}
secondActivity //
package com.example.me.rateme;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.Set;

public class Show extends Activity {
Set<String> s;
private static final String Myshared="mypreferance";
SharedPreferences shared = this.getSharedPreferences(Myshared, Context.MODE_PRIVATE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);

Set<String>set1=shared.getStringSet("name",null);
Set<String>set2=shared.getStringSet("rate",null);
Set<String>set3=shared.getStringSet("comment",null);
String namelist[] = new String[set1.size()];
String ratelist[] = new String[set2.size()];
String commentlist[] = new String[set3.size()];
namelist=set1.toArray(namelist);
ratelist=set1.toArray(ratelist);
commentlist=set1.toArray(commentlist);

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,namelist);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ratelist);
ListView l1 = (ListView) findViewById(R.id.Listview1);
ListView l2 = (ListView) findViewById(R.id.Listview1);
l1.setAdapter(adapter1);
l2.setAdapter(adapter2);
}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Whitney · February 9, 2016
Further down the stack trace (where you found the error you posted), it will point to the origin of the error i.e. in your  code

I suspect it is this line:
SharedPreferences shared =this.getSharedPreferences(Myshared, Context.MODE_PRIVATE);

I say that because of the "= this." which I interpret to mean that your code has defined something.

If this is the offending line pointed out in the stack trace, try eliminating the "this."
  • 1

Java / Android Development

106,921 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator