java.lang.NullPointer exception in GetResources().getString(R.string.value);

java.lang.NullPointer exception in GetResources().getString(R.string.value); … here is a solution to the problem.

java.lang.NullPointer exception in GetResources().getString(R.string.value);

I

want a constant value in all activities, so I write it in a string.xml file.

Now, to get the value in the .java file, I’m writing code like this:

getResources().getString(R.string.my_website); - This is 18 line of Splash Class

I call this code in the main class as follows:

public class Splash extends Activity {
    final String WebsiteURL = getResources().getString(R.string.my_website);
}

But it gives me java.lang.NullPointerException. Did I write incorrectly?

This is the relevant strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="my_website">http://www.mytestbuddy.com</string>
</resources>

Log:

01-30 17:57:15.853: E/AndroidRuntime(1553): FATAL EXCEPTION: main
01-30 17:57:15.853: E/AndroidRuntime(1553): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com. Mobile.mytestbuddy/com. Mobile.mytestbuddy.Splash}: java.lang.NullPointerException
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.os.Looper.loop(Looper.java:130)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at java.lang.reflect.Method.invoke(Method.java:507)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at dalvik.system.NativeStart.main(Native Method)
01-30 17:57:15.853: E/AndroidRuntime(1553): Caused by: java.lang.NullPointerException
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at com. Mobile.mytestbuddy.Splash.<init>(Splash.java:18)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at java.lang.Class.newInstanceImpl(Native Method)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at java.lang.Class.newInstance(Class.java:1409)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-30 17:57:15.853: E/AndroidRuntime(1553):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-30 17:57:15.853: E/AndroidRuntime(1553):     ... 11 more

Solution

Based on your edits, you are trying to access the resource before creating the activity. This is not possible because the required Context is not yet available at this time.

You need to set your websiteURL in the onCreate(...) method.

–Old answer for quote–

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

As you can see, string must be written in lowercase letters. Also, instead of looking for Integer, you should look for String.

Related Problems and Solutions