java.lang.NullPointer Exception in getResources().getString(R.string.value);

android, java, xml

Solution

According to your edits you're trying to access the resources before the Activity has been created. This is not possible, since the required `Context` is not available yet during that point.

You'll need to set your `websiteUrl` from within the `onCreate(...)` method.

--Old answer for reference--

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

As you can see, `string` has to be written in lowercase. Also you should not be looking for an `Integer` but a `String`.

Problem

I want one constant value in all activities so I have written it in string.xml file. Now, to get that value in .java file, I am writing code as below : ``` getResources().getString(R.string.my_website); - This is 18 line of Splash Class ``` I am calling this code in main class as below : ``` public class Splash extends Activity { final String WebsiteURL = getResources().getString(R.string.my_website); } ``` but it is giving me `java.lang.NullPointerException`. Am I writing anything wrong? This is the correlating strings.xml: ``` <?xml version="1.0" encoding="utf-8"?> <resources> <string name="my_website">http://www.mytestbuddy.com</string> </resources> ``` Logcat: ``` 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 ```

Original source

Related problems