title: Receiving 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
body: <p>I am new to android development and scripting with java. I am trying to create an app that allows me to toggle the internal microphone on and off via a switch. I found two scirpts (a switch script and a microphone control script) and I have peaced them together, correcting any debugging issues as I go and currently the script shows up clean. However when it is run on a phone or simulator it crashes immediately posting the following error,</p>
  
  <blockquote>
    <p>android.content.Context android.content.Context.getApplicationContext()' on a null object reference"focused around my use of import android.content.Context;. </p>
  </blockquote>
  
  <p>The error appears to be based on the Context.getApplicationContext request failing to find a result and thus posting NULL. </p>
  
  <p>I have looked at a long list of other people suffering from similar areas, but all solutions seem to focus on changes to different areas of their code which I cannot relate back to my own scripts.</p>
  
  <pre><code>package com.example.myfirstapp;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.content.Context;
  import android.media.AudioManager;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Switch;
  
  public class MainActivity extends AppCompatActivity {
  
      private Switch sw1;
      Button btnGet;
      Context context = getApplicationContext();
      AudioManager audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          sw1 = findViewById(R.id.switch1);
          btnGet = findViewById(R.id.getBtn);
          btnGet.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (sw1.isChecked()) {
                      audioManager.setMicrophoneMute(false);
                  }
                  else {
                      audioManager.setMicrophoneMute(true);
                  }
  
              }
          });
      }
  }
  </code></pre>
  
  <p>The expected result, even if the script doesn't work, is that I am able to run the app, currently the app crashes on startup and posts the following error message...</p>
  
  <p>[Logcat]</p>
  
  <pre><code>2019-08-20 13:23:01.710 5559-5559/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.myfirstapp, PID: 5559
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
          at android.app.ActivityThread.-wrap12(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          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:866)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
          at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
          at com.example.myfirstapp.MainActivity.&lt;init&gt;(MainActivity.java:16)
          at java.lang.Class.newInstance(Native Method)
          at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          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:866) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
  </code></pre>