{"id":1414,"date":"2022-08-30T15:16:20","date_gmt":"2022-08-30T15:16:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/android-pick-image-from-gallery-nullpointerexception-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:20","modified_gmt":"2022-08-30T15:16:20","slug":"android-pick-image-from-gallery-nullpointerexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-pick-image-from-gallery-nullpointerexception-collection-of-common-programming-errors\/","title":{"rendered":"Android pick image from Gallery NullPointerException?-Collection of common programming errors"},"content":{"rendered":"<p>I am currently trying to create an App, which allows the user to pic an image from gallery and upload it to a server. But i&#8217;m still stuck in picking an image. I followed a tutorial exactly, but when launching the activity i get a NullPointer.<\/p>\n<p>Do you guys see any errors?<\/p>\n<p>Thanks in advance!<\/p>\n<p>Heres the code;<\/p>\n<pre><code>package de.arvidg.exampleactionbartabs;\n\nimport android.app.Activity;\nimport android.content.Intent;\nimport android.database.Cursor;\nimport android.graphics.BitmapFactory;\nimport android.net.Uri;\nimport android.os.Bundle;\nimport android.provider.MediaStore;\nimport android.view.View;\nimport android.widget.Button;\nimport android.widget.ImageView;\n\n\npublic class UploadPicture extends Activity {\n\n    private static int RESULT_LOAD_IMAGE = 1;\n\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.main);\n\n        Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);\n        buttonLoadImage.setOnClickListener(new View.OnClickListener() {\n\n            @Override\n            public void onClick(View arg0) {\n\n                Intent i = new Intent(\n                        Intent.ACTION_PICK,\n                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);\n\n                startActivityForResult(i, RESULT_LOAD_IMAGE);\n            }\n        });\n    }\n\n\n    @Override\n    protected void onActivityResult(int requestCode, int resultCode, Intent data) {\n        super.onActivityResult(requestCode, resultCode, data);\n\n        if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) {\n            Uri selectedImage = data.getData();\n            String[] filePathColumn = { MediaStore.Images.Media.DATA };\n\n            Cursor cursor = getContentResolver().query(selectedImage,\n                    filePathColumn, null, null, null);\n            cursor.moveToFirst();\n\n            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);\n            String picturePath = cursor.getString(columnIndex);\n            cursor.close();\n\n            ImageView imageView = (ImageView) findViewById(R.id.chosenPicView);\n            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));\n\n        }\n\n\n    }\n}\n<\/code><\/pre>\n<p>LogCat:<\/p>\n<pre><code>03-17 10:56:35.470: W\/dalvikvm(12169): threadid=1: thread exiting with uncaught exception (group=0x41e64300)\n03-17 10:56:35.480: E\/AndroidRuntime(12169): FATAL EXCEPTION: main\n03-17 10:56:35.480: E\/AndroidRuntime(12169): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.arvidg.exampleactionbartabs\/de.arvidg.exampleactionbartabs.UploadPicture}: java.lang.NullPointerException\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread.access$600(ActivityThread.java:145)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.os.Handler.dispatchMessage(Handler.java:99)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.os.Looper.loop(Looper.java:137)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread.main(ActivityThread.java:4978)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at java.lang.reflect.Method.invokeNative(Native Method)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at java.lang.reflect.Method.invoke(Method.java:511)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at dalvik.system.NativeStart.main(Native Method)\n03-17 10:56:35.480: E\/AndroidRuntime(12169): Caused by: java.lang.NullPointerException\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at de.arvidg.exampleactionbartabs.UploadPicture.onCreate(UploadPicture.java:34)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.Activity.performCreate(Activity.java:5008)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)\n03-17 10:56:35.480: E\/AndroidRuntime(12169):    ... 11 more\n<\/code><\/pre>\n<p><strong>main.xml<\/strong><\/p>\n<pre><code>\n\n\n    \n\n        \n\n        \n\n        \n\n    \n\n\n\n<\/code><\/pre>\n<p>Line 34 is the last bracket of that statement<\/p>\n<pre><code>buttonLoadImage.setOnClickListener(new View.OnClickListener() {\n\n            @Override\n            public void onClick(View arg0) {\n\n                Intent i = new Intent(\n                        Intent.ACTION_PICK,\n                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);\n\n                startActivityForResult(i, RESULT_LOAD_IMAGE);\n            }\n        });\n<\/code><\/pre>\n<ol>\n<li>\n<p>just change<\/p>\n<pre><code>Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);\n<\/code><\/pre>\n<p>to<\/p>\n<pre><code>Button buttonLoadImage = (Button) findViewById(R.id.selectpic);\n<\/code><\/pre>\n<p>at some point in your code you have changed the id of your button in the layout file and didn&#8217;t reflect that change on the activity code.<\/p>\n<p>always double check your ids.<\/p>\n<\/li>\n<li>\n<p>Your mistake is i think in front of you. You have given wrong id of the <code>Button<\/code> and <code>ImageView<\/code> also.<\/p>\n<p>Initialize your button with the id which you have given in your layout as below: Define your button id as <code>R.id.selectpic<\/code>.<\/p>\n<blockquote>\n<pre><code>  Button buttonLoadImage = (Button) findViewById(R.id.selectpic);\n<\/code><\/pre>\n<\/blockquote>\n<p>And your <code>ImageView<\/code> id as <code>R.id.imageView1<\/code> besides <code>R.id.chosenPicView<\/code> as below:<\/p>\n<blockquote>\n<pre><code> ImageView imageView = (ImageView) findViewById(R.id.imageView1); \n<\/code><\/pre>\n<\/blockquote>\n<p>And then try out.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:49:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am currently trying to create an App, which allows the user to pic an image from gallery and upload it to a server. But i&#8217;m still stuck in picking an image. I followed a tutorial exactly, but when launching the activity i get a NullPointer. Do you guys see any errors? Thanks in advance! [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1414","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1414"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1414\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}