0

I've added a boolean var to my strings.xml, but when I am trying to retrieve it with the following code.

boolean custom = getResources().getBoolean(R.bool.customMade);

I am getting a nullpointerexception:

06-13 11:01:45.983: W/System.err(11951): java.lang.NullPointerException
06-13 11:01:45.983: W/System.err(11951):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
06-13 11:01:45.993: W/System.err(11951):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.tools.ConfigFeatures.<init>(ConfigFeatures.java:32)
06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.erasmus.actLogin.<init>(actLogin.java:50)
06-13 11:01:46.003: W/System.err(11951):    at java.lang.Class.newInstanceImpl(Native Method)
06-13 11:01:46.013: W/System.err(11951):    at java.lang.Class.newInstance(Class.java:1319)
06-13 11:01:46.013: W/System.err(11951):    at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
06-13 11:01:46.013: W/System.err(11951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
06-13 11:01:46.013: W/System.err(11951):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
06-13 11:01:46.023: W/System.err(11951):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
06-13 11:01:46.023: W/System.err(11951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
06-13 11:01:46.023: W/System.err(11951):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 11:01:46.023: W/System.err(11951):    at android.os.Looper.loop(Looper.java:137)
06-13 11:01:46.033: W/System.err(11951):    at android.app.ActivityThread.main(ActivityThread.java:5293)
06-13 11:01:46.033: W/System.err(11951):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 11:01:46.033: W/System.err(11951):    at java.lang.reflect.Method.invoke(Method.java:511)
06-13 11:01:46.033: W/System.err(11951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-13 11:01:46.033: W/System.err(11951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-13 11:01:46.043: W/System.err(11951):    at dalvik.system.NativeStart.main(Native Method)

this is where I add the var in my strings.xml:

<resources>
<bool name="customMade">true</bool>
</resources>

The strings.xml is localized in the res/values/strings.xml folder as usual. Anyone who can see what I am doing wrong? Thanks in advance

@edit:

it seems that I've posted not enough code, so here is the whole class in which I trying to retrieve the bool var from my resources:

import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;

public class ConfigFeatures extends Activity{

//public vars
Cursor mCursor;
public String HeaderIconColor;
public String HeaderStateColor;
public String BackgroundColor;
public String TextColor;
public String DateColor;
public String OrderColorOne;
public String OrderColorTwo;
public int Mijlpaal1;
public int Mijlpaal2;
public Boolean Ritten;
public Boolean Handtekening;
public Boolean Foto;
public Boolean WijzigOrderDetail;
public Boolean Firma;
public Boolean LocatieGegevens;
public int LocatieInterval;

public ConfigFeatures(){
    //initialize the vars

    try {
        boolean custom = getResources().getBoolean(R.bool.customMade);
        if (custom) {
            DefaultSettings();
        } else {
            try{
                mCursor = Session.globalTDBAdapter().getData("tbl_Setting", new String[]{TDBAdapterSingleton.KEY_HEADERICONCOLOR,
                        TDBAdapterSingleton.KEY_HEADERSTATECOLOR,TDBAdapterSingleton.KEY_BACKGROUNDCOLOR,
                        TDBAdapterSingleton.KEY_TEXTCOLOR,TDBAdapterSingleton.KEY_DATECOLOR,
                        TDBAdapterSingleton.KEY_ORDERCOLORONE,TDBAdapterSingleton.KEY_ORDERCOLORTWO,
                        TDBAdapterSingleton.KEY_MIJLPAAL1, TDBAdapterSingleton.KEY_MIJLPAAL2,
                        TDBAdapterSingleton.KEY_RITTEN, TDBAdapterSingleton.KEY_HANDTEKENING,
                        TDBAdapterSingleton.KEY_FOTO,TDBAdapterSingleton.KEY_FIRMA,
                        TDBAdapterSingleton.KEY_WIJZIGORDERDETAIL,TDBAdapterSingleton.KEY_LOCATIEGEGEVENS,
                        TDBAdapterSingleton.KEY_LOCATIEINTERVAL});

                if (mCursor == null){
                    DefaultSettings();
                } else {
                    this.HeaderIconColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HEADERICONCOLOR));
                    if (this.HeaderIconColor.equals("")){
                        this.HeaderIconColor = "#FFFFFF";
                    }
                    this.HeaderStateColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HEADERSTATECOLOR));
                    if (this.HeaderStateColor.equals("")){
                        this.HeaderStateColor = "#FF8000";
                    }
                    this.BackgroundColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_BACKGROUNDCOLOR));
                    if (this.BackgroundColor.equals("")){
                        this.BackgroundColor = "#FFFFFF";
                    }
                    this.TextColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_TEXTCOLOR));
                    if (this.TextColor.equals("")){
                        this.TextColor = "#000000";
                    }
                    this.DateColor = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_DATECOLOR));
                    if (this.DateColor.equals("")){
                        this.DateColor = "#808080";
                    }
                    this.OrderColorOne = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_ORDERCOLORONE));
                    if (this.OrderColorOne.equals("")){
                        this.OrderColorOne = "#EBE3E3";
                    }
                    this.OrderColorTwo = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_ORDERCOLORTWO));
                    if (this.OrderColorTwo.equals("")){
                        this.OrderColorTwo = "#FFFFFF";
                    }
                    this.Mijlpaal1 = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_MIJLPAAL1));
                    this.Mijlpaal2 = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_MIJLPAAL2));
                    this.Ritten = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_RITTEN)).contains("0");
                    this.Handtekening = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_HANDTEKENING)).contains("0");
                    this.Foto = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_FOTO)).contains("0");
                    this.WijzigOrderDetail = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_WIJZIGORDERDETAIL)).contains("0");
                    this.Firma = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_FIRMA)).contains("0");
                    this.LocatieGegevens = mCursor.getString(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_LOCATIEGEGEVENS)).contains("0");
                    this.LocatieInterval = mCursor.getInt(mCursor.getColumnIndexOrThrow(TDBAdapterSingleton.KEY_LOCATIEINTERVAL));
                    mCursor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                mCursor.close();
            } finally {
                DefaultSettings();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        DefaultSettings();
    }
}
public void DefaultSettings(){
    this.HeaderIconColor = "#FFFFFF";
    this.HeaderStateColor = "#3DA2C8";
    this.BackgroundColor = "#FFFFFF";
    this.TextColor = "#000000";
    this.DateColor = "#808080";
    this.OrderColorOne = "#3DA2C8";
    this.OrderColorTwo = "#FFFFFF";
    this.Mijlpaal1 = 1;
    this.Mijlpaal2 = 2;
    this.Ritten = false;
    this.Handtekening = true;
    this.Foto = true;
    this.WijzigOrderDetail = true;
    this.Firma = true;
    this.LocatieGegevens = true;
    this.LocatieInterval = 1000 * 60 * 5;
}

}

10
  • where do use this boolean custom = getResources().getBoolean(R.bool.customMade); in your activity class? Commented Jun 13, 2013 at 9:08
  • I am using the boolean just 2 lines below that line of code. For an if statement Commented Jun 13, 2013 at 9:20
  • 1
    do you use the same in activity class or non activity class Commented Jun 13, 2013 at 9:21
  • the class is a not an activity class Commented Jun 13, 2013 at 9:25
  • Then that is the issue my friend, it has to be an activity class OR you can pass an activity reference then do something like this: activity.getResources().getBoolean(R.bool.customMade); aside from that everything looks fine. Commented Jun 13, 2013 at 9:26

6 Answers 6

1

Change it to:

public ConfigFeatures(Activity activity){
    boolean custom = activity.getResources().getBoolean(R.bool.customMade);

and the activity calling ConfigFeatures() make it like this ConfigFeatures(getBaseContext()

Sign up to request clarification or add additional context in comments.

2 Comments

that would do the trick too indeed, but I've implemented the onCreate and it does the job too. So thanks a lot..
you are welcome, however I would recommend you pass an activity/context, and not implement an activity where an activity is not really there, I think that may have unpredictable side-effects, it just seem iffy. Cheers and good luck :)
0

I you wanted something into strings.xml, you should have wrote

getResources().getString(R.string.customMade);

This is where your NullPointerException comes from. On the other hand, don't think you are able to put boolean or other values into strings.xml

Comments

0

The getResources() returns null, because it cannot find your file.

The first obvious one is you are pointing to R.bool while your bool is in R.string

Either put your bool in a bools.xml or use R.string (better just use bools.xml).

Note:

@Raghunandan asks a volid question because it is also possible getResources() is missing context.

Depending on where you place the call, getResource() also returns null when you dont pass your Context as reference. For example: getApplicationContext().getResources();

Comments

0

Do it like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <item type="bool" name="customMade">true</item>
</resources>

Then in the code get the value by doing this:

boolean custom= getResources().getBoolean(R.bool.customMade);

Comments

0

You should have this /res/values/bools.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="customMade">true</bool>
</resources>

To refer

Resources res = getResources();
boolean custom  = res.getBoolean(R.bool.customMade);

http://developer.android.com/guide/topics/resources/more-resources.html#Bool

If you are using getResources() in a non activity class you need the context . pass the context of the activity to the non activity class constructor and use the same

Form you activity

   new  ConfigFeatures(ActivityName.this);

In your non activity clas

   Context mContext;  
   public ConfigFeatures(Context c){
     mContext =c; // use the context to get resources

   }

2 Comments

I doubt that creating an object of Resources will do the trick, because I am doing the same thing. Only now I am doing it in 1 line of code. But I will try this solution
@user where do you call the same in your activity class or a class that doe snot extend activity?
0

You are accessing your resources too early, as indicated by this line in the stacktrace:

 06-13 11:01:46.003: W/System.err(11951):    at com.koeriers.erasmus.actLogin.<init>(actLogin.java:50)

That means the initialization code of your actLogin. Move your member variable init code that requires accessing resources to onCreate().

-- edit after question updated

Your ConfigFeatures is not an activity even though it extends it. It therefore does not have a valid Context for getResources() to work. Solution: Remove extends Activity and add a Context parameter to the constructor, calling getResources() on the passed-in context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.