4

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
1
  • I've updated my answer to fit your edits. Commented Jan 30, 2014 at 12:36

3 Answers 3

7

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.

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

2 Comments

sorry for my mistake, but I am already writing string in lower case.
That's good. The app should not have been compiling if you used uppercase anyways. Nonetheless the question implied that this is part of the issue in the first place.
2

you must use

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

change getInteger with getString and String with string (lowercase s)

You can't call getString before your Activity has been initialized. That's because getString is the same as context.getResources().getString(). And context is not initialized

for more info please see this

6 Comments

thank you. It was my mistake in post. I am writing getString but then also I am getting same error.
please clean your project and try again
getResources().getString(R.string.my_website); is the line of 18 of Splash class
Yes, I did it and run again. Even I close Emulator and started again but same error.
I am calling this in main class, check updated post.
|
0

You need put getResources().getString(R.string.my_website); after

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

to access the resources, because Activity need init.

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.