2

I am receiving a null pointer exception for some reason. My app crashes when trying to load strings from the strings xml file. I have no idea why there is a null pointer. Any ideas? Thanks!

Quiz.java snippet:

  String[] questions = {getString(R.string.q1),
            getString(R.string.q2),
            getString(R.string.q3),
            getString(R.string.q4),
            getString(R.string.q5),getString(R.string.q6),
            getString(R.string.q7),
            getString(R.string.q8),
            getString(R.string.q9),
            getString(R.string.q10),


    };   

strings.xml:

<resources>
        <string name="app_name">Beatles Quiz</string>
        <string name="q1">What year did John say the Beatles were more popular than Jesus?</string>
        <string name="q2">What did the Beatles receive for performing three times on The Ed Sullivan Show?</string>
        <string name="q3">Who sent a congratulatory telegram to the Beatles before that first performance?</string>
        <string name="q4">Which song starts "It was 20 years ago today …"?"</string>
        <string name="q5">Who was the Beatles\' original drummer?</string>
        <string name="q6">Which song starts "When I find myself in times of trouble …"?</string>
        <string name="q7">Which pair of Beatles first played together in a band?"</string>
        <string name="q8">Which song starts: "I think I\'m gonna be sad …"?</string>
        <string name="q9">Who was the youngest Beatle?"</string>
        <string name="q10">Which song starts: "Well, she was just 17 …"?"</string>

        <string name="q11">Besides Liverpool, where did the Beatles perform regularly in the early 1960s?</string>
        <string name="q12">Which song starts: "There are places I remember …"?</string>
        <string name="q13">Which album cover features 73 people?"</string>
        <string name="q14">Which song starts: "Try to see it my way …"?"</string>
        <string name="q15">What\'s the official name of the album best known as "The White Album"?</string>
        <string name="q16">Which song starts: "In the town where I was born …"?</string>
        <string name="q17">Who was the first Beatle to get married?</string>
        <string name="q18">Which song starts: "What would you do if I sang out of tune …"?</string>
        <string name="q19">The Beatles got their MBEs in 1965. Which of them later returned his?</string>
        <string name="q20">What was the last Beatles studio album to be recorded, though it was not the last one released?</string>

        <string name="q21">What do the songs "Drive My Car," "Taxman," "Back in the USSR" and "Come Together" have in common?</string>
        <string name="q22">Which song includes the lyric "Man, you should have seen them kicking Edgar Allan Poe"?</string>
        <string name="q23">Which Broadway show tune did the Beatles cover in their early days?</string>
        <string name="q24">The working title of "Yesterday" was:</string>
        <string name="q25">Which was the last Beatles album to be released?</string>
        <string name="q26">Which ex-Beatle had the first No. 1 hit single on the Billboard Hot 100?</string>
        <string name="q27">What was the working title of the film "Help!"?"</string>
        <string name="q28">Which was the first song Ringo alone wrote for the Beatles?</string>
        <string name="q29">What year did the Beatles stop touring?</string>
        <string name="q30">Frank Sinatra considered this Beatles song (which he also performed repeatedly in concert)"the greatest love song of the past 50 years":</string>

        <string name="q31">For whom did Paul McCartney write "Hey Jude"?</string>
        <string name="q32">Which Beatle is barefoot on the Abbey Road cover?</string>
        <string name="q33">Who does not appear on the cover of Sgt. Pepper\'s Lonely Hearts Club Band?</string>
        <string name="q34">Which song includes the lyric "Everywhere, people stare"?"</string>
        <string name="q35">Which song was NOT on the setlist during the Beatles’ Ed Sullivan show appearance?</string>
        <string name="q36">The album "Please Please Me" contains the first Beatles song featuring George Harrison on lead vocals. Which song was it?</string>
        <string name="q37">Which Beatle is the only one to have been knighted?</string>
        <string name="q38">The album "Rubber Soul" features the first song where credit was given to Ringo Starr as a co-songwriter. Which song did Ringo lend his efforts to?</string>
        <string name="q39">Which Beatles song contains the lyrics "By making his world a little colder"</string>
        <string name="q40">Which Beatles song contains the lyrics "Nobody knows, just we two"?"</string>

         <string name="q41">Which song from Let It Be was actually one of the earliest Lennon/McCartney compositions?</string>
         <string name="q42">Which song includes the lyric "The farther one travels, the less one knows"?"</string>
        <string name="q43">Which John Lennon track combined two takes in two different keys?</string>
        <string name="q44">Which song includes the lyric "Drove from Paris to the Amsterdam Hilton"?"</string>
        <string name="q45">The Revolver track “For No One” features a solo on what instrument?</string>
        <string name="q46">Which song includes the lyric "I got something to say that might cause you pain"?"</string>
        <string name="q47">Who is "I’m Looking Through You" about?"</string>
        <string name="q48">Which song includes the lyric "Everybody had a great year, everybody let their hair down"?"</string>
        <string name="q49">Who plays bass on "The Long and Winding Road"?"</string>
        <string name="q50">Which song includes the lyric "The teachers who taught me weren\'t cool"?"</string>
    </resources>

The error message:

05-11 23:23:06.652 26077-26077/tuxstudios.com.beatlesquiz E/AndroidRuntime: FATAL EXCEPTION: main Process: tuxstudios.com.beatlesquiz, PID: 26077 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tuxstudios.com.beatlesquiz/tuxstudios.com.beatlesquiz.QuizActivity2}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2321) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5692) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:94) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) at android.content.Context.getString(Context.java:363) at tuxstudios.com.beatlesquiz.QuizActivity2.(QuizActivity2.java:32) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)  at android.app.ActivityThread.access$900(ActivityThread.java:172)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5692)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)  at dalvik.system.NativeStart.main(Native Method)

4 Answers 4

3

You should use

int [] stringIds = { R.string.q1, R.string.q2... }

As when you declare the string [], you call getString(id), where some context or resource is needed, but not yet initialized, that accounts for the null error.

e.g.

getResources().getString(R.string.q1); //here getResources() may be null.

Then after your context (e.g. activity) is created, call below function

for( int id : stringIds)
    String question = getResources().getString(id); //now this is no longer null

The difference is R.string.q1 always accessible, either at compile time or at runtime.

Hope this helps.

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

3 Comments

If I were to place the strings into a string array, I would do this, correct? for( int id : stringIds) questions[id] = getResources().getString(id);
no, id is not the index of the array, but the integer ID of a string.
I see. I went with the other answer as that worked. Is there any reason to use this method over creating array in the on create method?
1

You're calling getResources() from a field initializer. Something like

private String mStr = getResources().getString(...);

(or with drawables or other kind of resources).

This is not valid, since the Context isn't setup yet. Move this assignment inside the onCreate() method.

In your case:

String[] questions;

and In onCreate();

questions = { 
            getResources().getString(R.string.q1),
            getResources().getString(R.string.q2),
            getResources().getString(R.string.q3),
            getResources().getString(R.string.q4),
            getResources().getString(R.string.q5),
            getResources().getString(R.string.q6),
            getResources().getString(R.string.q7),
            getResources().getString(R.string.q8),
            getResources().getString(R.string.q9),
            getResources().getString(R.string.q10)
          }

Comments

0

try this

String value= getResources().getString(R.string.q1);

for reference click here

Comments

0

Why don't you create a string-array instead of multiple strings?

<resources>
    <string-array name="questions">
        <item>What year did John say the Beatles were more popular than Jesus?</item>
        <item>What did the Beatles receive for performing three times on The Ed Sullivan Show?</item>
        <!-- ... -->
    </string-array>
</resources>

And then just get it like that:

String[] questions = getResources().getStringArray(R.array.questions);

Also, of course, move your code from initializer to onCreate() or where you can get your 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.