NullPointerException Android getContext()
<p>I am using Android API 23. I'm trying to request permissions from the user on runtime as is recommended for the new API. This is my implementation.</p>

<pre><code>private void requestPermission(String type){
    String permission = "";
    String requestText = "";
    if(type.equals("video")){
        permission = Manifest.permission.CAMERA;
        requestText = "Need Camera Permission";
    }else if(type.equals("picture")){
        permission = Manifest.permission.CAMERA;
        requestText = "Need Camera Permission";
    }else if(type.equals("audio")){
        permission = Manifest.permission.RECORD_AUDIO;
        requestText = "Need Audio Permission";
    }

    if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
        Snackbar.make(view, requestText, Snackbar.LENGTH_INDEFINITE).setAction("Ok",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
                    }
                });
    }else {
        Snackbar.make(view, "Permission is not available. Requesting camera permission.", Snackbar.LENGTH_SHORT).show();
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
    }
}
</code></pre>

<p>On running the code on the phone, I get the following error in Android Studio. </p>

<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
</code></pre>

<p>The full traceback is here:</p>

<pre><code>FATAL EXCEPTION: main
Process: com.xxxxx.gcmtest, PID: 4779
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.support.design.widget.Snackbar.&lt;init&gt;(Snackbar.java:183)
at android.support.design.widget.Snackbar.make(Snackbar.java:215)
at com.xxxxx.gcmtest.ConversationActivity.requestPermission(ConversationActivity.java:271)
at com.xxxxx.gcmtest.ConversationActivity.onClick(ConversationActivity.java:185)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
</code></pre>

<p>Line 271 of <code>requestPermission</code> is:</p>

<pre><code>Snackbar.make(view,requestText,Snackbar.LENGTH_INDEFINITE).setAction("Ok",...);
</code></pre>

<p>My <code>onCreate</code> looks like this:</p>

<pre><code>private ChatAdapter chatAdapter;
private ListView listView;
private Button buttonSendAudio;
private Button buttonSendVideo;
private Button buttonSendPicture;
private boolean side = false;
private String fileType;
private Context context;
private static final int PERMISSION_REQUEST_CODE = 1;
private View view;
private Activity activity;
view = findViewById(R.id.chat_layout);
</code></pre>

<p>My <code>fragment.xml</code>:</p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/chat_layout"&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" /&gt;
    &lt;ListView
        android:id="@+id/list_conversations"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="80dp"&gt;
    &lt;/ListView&gt;

    &lt;RelativeLayout
        android:id="@+id/form"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" &gt;

    &lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>Where am I going wrong and how do I fix this? </p>