1

I am Building an Npuzzle App.
The current issue I face is bringing a drawable object into my Activity so that I can convert it to a bitmap for splitting. I have found and attempted to use this Approach:

Drawable hold = Puzzle.this.getResources().getDrawable(R.drawable.img1);
BitmapDrawable hold2 = (BitmapDrawable) hold;
Bitmap bitPhoto = hold2.getBitmap();

When I run my app I get a null pointer exception in the first line of code. Any suggestions on how to fix this problem? this is the logcat when i run the program:

 11-25 12:07:46.441    5388-5388/com.example.anthony.puzzler E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.anthony.puzzler/com.example.anthony.puzzler.Puzzle}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
        at com.example.anthony.puzzler.Puzzle.<init>(Puzzle.java:28)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
            at dalvik.system.NativeStart.main(Native Method)
2
  • Why do you get the null ptr exception? What is null that you are invoking or trying to access its member? Don't stop at the first exception, seek what is causing it. What is null that you are invoking or accessing its member? Commented Nov 25, 2014 at 16:25
  • '11-25 12:07:46.441 5388-5388/com.example.anthony.puzzler E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.anthony.puzzler/com.example.anthony.puzzler.Puzzle}: java.lang.NullPointerException Caused by: java.lang.NullPointerException' Commented Nov 25, 2014 at 17:11

2 Answers 2

1

Clean up your project and rebuild it to make sure the id of the drawable is the correct one.

Check if getResources() is actually returning something.

Edit: calling getResources() before super.onCreate() method returns, it causes NPE.

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

5 Comments

this is a line from the log cat: Caused by: 'java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:81)'
Please post all of the error from logcat in your question.
Where are you calling getResources() from? In which method? If you call it before super.onCreate() returns you get a NPE
Thank you that solved my error and it makes a lot of sense ... rookie mistake
I see, I will edit my answer with that in consideration
1

Why don't you just directly get the media as a bitmap?

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.img1);

Should be simpler than getting a drawable and casting it.

6 Comments

If his implementation is throwing NPE then this one will also because of getResources() or the drawable id is wrong.
If the NPE is at the first line, yes. All in all, if this code is executed in a subclass of Activity (or any class with Context), getResources() should not return null. Your suggestions are a good idea in that case. I was just trying to help him simplify his "resource => drawable => bitmapdrawable => bitmap" casts to a more simple and manageable "resource => bitmap".
thanks i have implemented your approach ... it does not solve the issue as predicted but it does help
I am working in a class Puzzle which extends Activity
Make sure your res folder is in the class path, verify using project properties > Java build Path > Source tab.
|

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.