0

I'm developing an app in android that uses gestures to represent letters. I have an element of a string array displayed in the text area so the user knows what letter to draw. I declare this globally:

    Resources res = getResources();
String [] letters = res.getStringArray(R.array.LetterToBeDrawn)

Then I have this in my oncreate:

TextView tv = (TextView) findViewById(R.id.text);
tv.setText("Draw the letter: "+letters[i]);

And in my gesturePerformed method I've got this comparison:

if (predictions.size() > 0)
{
    Prediction prediction = predictions.get(0);
    if (prediction.score > 1.0 && prediction.name.equals(letters[i]))
    {
        Toast.makeText(MainActivity.this, prediction.name, Toast.LENGTH_SHORT).show();
    }
}

When I go to launch this on my emulator I get a lot of errors in the LogCat, and they seem to be caused by a NullPointerException. But I can't see how that could be as the array at the moment only contains three items and the index int i is set to 1.

EDIT: Here's the LogCat, meant to post it earlier:

04-25 15:37:31.266: D/AndroidRuntime(1889): Shutting down VM
04-25 15:37:31.266: W/dalvikvm(1889): threadid=1: thread exiting with uncaught exception (group=0xb1a4eb90)
04-25 15:37:31.366: E/AndroidRuntime(1889): FATAL EXCEPTION: main
04-25 15:37:31.366: E/AndroidRuntime(1889): Process: com.gmail.Sheridjohn.letterchecker, PID: 1889
04-25 15:37:31.366: E/AndroidRuntime(1889): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gmail.Sheridjohn.letterchecker/com.gmail.Sheridjohn.letterchecker.MainActivity}: java.lang.NullPointerException
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.os.Looper.loop(Looper.java:137)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at java.lang.reflect.Method.invoke(Method.java:515)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at dalvik.system.NativeStart.main(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889): Caused by: java.lang.NullPointerException
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at com.gmail.Sheridjohn.letterchecker.MainActivity.<init>(MainActivity.java:21)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at java.lang.Class.newInstanceImpl(Native Method)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at java.lang.Class.newInstance(Class.java:1208)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-25 15:37:31.366: E/AndroidRuntime(1889):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
04-25 15:37:31.366: E/AndroidRuntime(1889):     ... 11 more

Here's my XML:

<android.gesture.GestureOverlayView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gestures"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        />

</android.gesture.GestureOverlayView>
9
  • 2
    Could you post your Logcat please? Commented Apr 25, 2014 at 19:28
  • 2
    When speaking of errors, please, post them along with all the code. Commented Apr 25, 2014 at 19:29
  • Are you getting a NPE at tv.setText()? Commented Apr 25, 2014 at 19:32
  • prediction is null? btw, please learn to use the debugger. These problems are the simplest of all bugs to find. Put a breakpoint on the first line of the method, run your app, step through until you see the problem. Job done. Commented Apr 25, 2014 at 19:34
  • Caused by: java.lang.NullPointerException 04-25 15:37:31.366: E/AndroidRuntime(1889): at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 04-25 15:37:31.366: E/AndroidRuntime(1889): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 04-25 15:37:31.366: E/AndroidRuntime(1889): at com.gmail.Sheridjohn.letterchecker.MainActivity.<init>(MainActivity.java:21) - what is line 21? Commented Apr 25, 2014 at 19:38

1 Answer 1

4

getResources() is null when you declare it globally. Move the initializations of both res and letters to your onCreate() method and keep the declarations where you currently have them.

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

1 Comment

Good spot. I missed that.

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.