0

so i am trying to use my shared prefference in a no activity class but i keep getting null pointer on my

private SharedPreferences setingPreferences = context.getSharedPreferences("Settings", Activity.MODE_PRIVATE);

how do i solve this?

 public class ConnectionClass
{
    Context context;
    private SharedPreferences setingPreferences = context.getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String ip;
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "ISTABLocalDB";
    String un = "istab_wpf";
    String password = "istab_!234";
    @SuppressLint("NewApi")
    public Connection CONN() {
        ip = setingPreferences.getString("server", "");
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;
        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + un + ";password="
                    + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("ERRO", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("ERRO", e.getMessage());
        } catch (Exception e) {
            Log.e("ERRO", e.getMessage());
        }
        return conn;
omini data
  • 407
  • 7
  • 29
  • can you post full class – Nayan Srivastava Aug 16 '16 at 17:23
  • how can that be a duplicate? as this is a nullpointer caused by a not initailized context to my sharedprefference and that one is just more a general question so is not helpfull at all and iknow what a nullpointer is – omini data Aug 16 '16 at 18:49

3 Answers3

2

You are using context before initializing it. In you class constructor take context as argument and then initialize you preference Object.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
2

You need to initialize your context variable. One way would be to pass the context variable from the activity class to this non-activity class by defining a constructor in the ConnectionClass

public ConnectionClass (Context context)  
{
   this.context = context;

} 

Wherever you are going to instantiate this class, instantiate it by passing the context from the calling class.

For example, if you are instantiating this from an activity class, you can do so by

ConnectionClass conn = new ConnectionClass(MyActivity.this.getApplicationContext());
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
0

You have to initialize context.

Get the context on the constructor like this:

Context context;
private SharedPreferences setingPreferences;

public ConnectionClass(Context context){
    this.context = context;
}

public Connection CONN() {
    setingPreferences = context.getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    ip = setingPreferences.getString("server", "");

    ...

Or you can get context direct on the method that you're calling:

private SharedPreferences setingPreferences;

public Connection CONN(Context context) {
    setingPreferences = context.getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    ip = setingPreferences.getString("server", "");

    ...
Ricardo A.
  • 685
  • 2
  • 8
  • 35
  • that are giving me Error:(427, 53) error: method CONN in class ConnectionClass cannot be applied to given types; required: Context found: no arguments reason: actual and formal argument lists differ in length – omini data Aug 16 '16 at 17:40
  • I think you're not passing the parameter when you call CONN(), or you're passing a different type. You have to call like this: Connection c = connectionClass.CONN(context); You're calling the method in a activity class? – Ricardo A. Aug 16 '16 at 17:48
  • i getting Error:(427, 53) error: method CONN in class ConnectionClass cannot be applied to given types; required: Context found: no arguments reason: actual and formal argument lists differ in length on `try { Connection c = connectionClass.CONN‌​(context); if (con == null) {` and it cannot resolve method and symbol – omini data Aug 16 '16 at 17:54
  • Well, this can be a lot of things, but I'll try to help. First, you need to have a context, if you're calling in a activity class you can pass like this: Context context = this; if you're not in a activity class you have to get in another way. Second, I think you're using wrong variable names, "c" and "con" is the same thing, isn't? If is you have to rename c to con or con to c. If the error persists tell me what IDE you're using and update the question with the code of the class that is calling the method CONN. – Ricardo A. Aug 16 '16 at 18:05