Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:44:18+00:00 2026-05-26T02:44:18+00:00

Why do I get an NPE at Drawable pic = (Drawable) i.getDrawable();?????? I am

  • 0

Why do I get an NPE at Drawable pic = (Drawable) i.getDrawable();??????
I am trying to get the height and width of a drawable in order to set its scale to be fit the XY of an imageview in an imageswitcher via a matrix (using a matrix to enable multitouch).

public View makeView() {

        ImageView i = new ImageView(this);
        Drawable pic = (Drawable) i.getDrawable();
        float displayHeight = i.getHeight();
        float imageHeight = pic.getIntrinsicHeight();
        float displayWidth = i.getWidth();
            float imageWidth = pic.getIntrinsicWidth();
        float scaleX = (float) displayWidth / (float) imageWidth;
        float scaleY = (float) displayHeight / (float) imageHeight;
        matrix.setScale(scaleX, scaleY);
        i.setBackgroundColor(0xFF000000);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        i.setImageMatrix(matrix);
        i.setScaleType(ScaleType.MATRIX);

Error Log:

E/AndroidRuntime(22016): FATAL EXCEPTION: main
E/AndroidRuntime(22016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ben.test/com.ben.test.ImageSwitch1}: java.lang.NullPointerException
E/AndroidRuntime(22016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(22016):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(22016):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E    /AndroidRuntime(22016):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(22016):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(22016):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(22016):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(22016):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(22016):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(22016):    at dalvik.system.NativeStart.main(Native Method)
E    /AndroidRuntime(22016): Caused by: java.lang.NullPointerException
E/AndroidRuntime(22016):    at com.ben.test.ImageSwitch1.makeView(ImageSwitch1.java:185)
E/AndroidRuntime(22016):    at android.widget.ViewSwitcher.obtainView(ViewSwitcher.java:80)
E/AndroidRuntime(22016):    at android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:99)
E/AndroidRuntime(22016):    at com.ben.test.ImageSwitch1.onCreate(ImageSwitch1.java:100)
E/AndroidRuntime(22016):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(22016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime(22016):    ... 11 more
W/ActivityManager( 1106):   Force finishing activity com.ben.test/.ImageSwitch1
W/ActivityManager( 1106):   Force finishing activity com.ben.test/.LP
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:44:19+00:00Added an answer on May 26, 2026 at 2:44 am

    getDrawable() is almost certainly returning a null value. Which isn’t a problem in itself, but then you get an NPE when you try to cast that null value to a Drawable.

    The underlying problem is that a newly created ImageView won’t have a drawable associated with it yet (as is evidenced by the fact that it returns null)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this NPE in my application and I can't seem to get
I get the following error when trying to run the latest Cygwin version of
I am trying to debug a NPE. I have used the following flag in
I'll make this quicky and easy. I get an NPE error... Row: 0 You
I'm trying to get a composite component working with it's own backing bean, using
I'm getting an NPE while trying to read in an image file, and I
I have some problems trying to get a bag collection of a class to
I'm trying to read a properties file /res/version.num . But all I get is
Why do I get an NPE on the assignment of: mPyramid[row][column] = temp; Here
I have a simple question, why do i always get a NPE when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.