Null pointer exception in setText : <p>I am trying to get name of a file using mediastore and display it in text view using <code>setText()</code> but <code>setText()</code> gives null pointer exception on running app. 
I am getting name from mediastore , saving it as string and passing the string to <code>setText()</code>. Can anyone help ? I am a beginner.</p>

<pre><code>case R.id.action_properties:
                AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this);
                LayoutInflater inflater = this.getLayoutInflater();
                builder.setMessage("Properties");
                builder.setView(R.layout.properties);
                String [] projection={MediaStore.Images.Media.DATA};
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                cursor = getApplication().getContentResolver().query(uri, projection, null, null, null);// Order-by clause (ascending by name)
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                TextView displayname = (TextView) findViewById(R.id.displayname);
                displayname.setText(cursor.getString(column_index));
                builder.setView(inflater.inflate(R.layout.properties, null));
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert12 = builder.create();
                alert12.show();
            // location found
                return true;
</code></pre>

<p>LOGCAT :</p>

<pre><code> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                at com.example.dell_1.myapp3.ImageViewer.PhotosActivity.onOptionsItemSelected(PhotosActivity.java:302)
                                                                                at android.app.Activity.onMenuItemSelected(Activity.java:3245)
                                                                                at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
                                                                                at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
                                                                                at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:206)
                                                                                at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
                                                                                at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:88)
                                                                                at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
                                                                                at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                                at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
                                                                                at android.widget.AbsListView$PerformClick.run(AbsListView.java:3133)
                                                                                at android.widget.AbsListView$3.run(AbsListView.java:4048)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
</code></pre>
