title: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo
body: <p>i am making a music player and it was working fine , but after some adjustment it started giving error in logcat : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
  the main_activity.java file is :</p>
  <pre><code>package com.example.musicplayer;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.RequiresApi;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.app.ActivityManager;
  import android.content.pm.PackageManager;
  import android.media.MediaPlayer;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Environment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.SeekBar;
  import android.widget.TextView;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  import java.util.Objects;
  
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      private static final String[] PERMISSIONS = {
              Manifest.permission.READ_EXTERNAL_STORAGE
      };
  
      private static final int REQUEST_PERMISSIONS=12345;
      private static final int permissions_count=1;
      @SuppressLint(&quot;NewApi&quot;)
      private boolean arepermissionsdenied(){
          for(int i=0;i&lt;permissions_count;i++){
              if(checkSelfPermission(PERMISSIONS[i])!= PackageManager.PERMISSION_GRANTED){
                  return true;
              }
          }
          return false;
      }
      @SuppressLint(&quot;NewApi&quot;)
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
          super.onRequestPermissionsResult(requestCode,permissions,grantResults);
          if(arepermissionsdenied()){
              ((ActivityManager)(this.getSystemService(ACTIVITY_SERVICE))).clearApplicationUserData();
              recreate();
          }else{
              onResume();
          }
      }
      private boolean isMusicPLayerInit;
      private List&lt;String&gt; musicFilesList;
      @RequiresApi(api = Build.VERSION_CODES.KITKAT)
      private void addMusicFilesFrom(String dirPath){
          final File musicDir=new File(dirPath);
          if(!musicDir.exists()){
             musicDir.mkdir();
             return;
          }
          if(musicDir==null){
              return;
          }
          final File[] files=musicDir.listFiles();
          for(File file: Objects.&lt;File[]&gt;requireNonNull(files)){
              final String path=file.getAbsolutePath();
              if(path.endsWith(&quot;.mp3&quot;)){
                  musicFilesList.add(path);
              }
          }
      }
      @RequiresApi(api = Build.VERSION_CODES.KITKAT)
      private void fillMusicList(){
          musicFilesList.clear();
          addMusicFilesFrom(String.valueOf(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_MUSIC)));
          addMusicFilesFrom(String.valueOf(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_DOWNLOADS)));
      }
      private MediaPlayer mp;
      private int playMusicFile(String path){
          mp =new MediaPlayer();
          try{
              mp.setDataSource(path);
              mp.prepare();
              mp.start();
          }catch(Exception e){
              e.printStackTrace();
          }
          return mp.getDuration();
      }
      private int songPosition;
      private boolean isSongPlaying=false;
      final View playbackControls=findViewById(R.id.playbackButtons);
  
      @Override
      protected void onResume(){
          super.onResume();
          if(Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.M &amp;&amp; arepermissionsdenied()){
              requestPermissions(PERMISSIONS,REQUEST_PERMISSIONS);
              return;
          }
          if(!isMusicPLayerInit){
              final ListView listview=findViewById(R.id.listview);
              final TextAdapter textAdapter=new TextAdapter();
              musicFilesList=new ArrayList&lt;&gt;();
              fillMusicList();
              listview.setAdapter(textAdapter);
              if(musicFilesList.isEmpty()){
                  textAdapter.setData(Collections.singletonList(&quot;please add files&quot;));
                  return;
              }else {
                  textAdapter.setData(musicFilesList);
              }
              final SeekBar seekBar=findViewById(R.id.seekBar);
              seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                  int songProgress;
                  @Override
                  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                      songProgress=progress;
                  }
  
                  @Override
                  public void onStartTrackingTouch(SeekBar seekBar) {
  
                  }
  
                  @Override
                  public void onStopTrackingTouch(SeekBar seekBar) {
                      songPosition=songProgress;
                      mp.seekTo(songPosition*1000);
                  }
              });
              final TextView songPositionTextView=findViewById(R.id.currentPosition);
              final TextView songDurationTextView=findViewById(R.id.songDuration);
              final Button pauseButton=findViewById(R.id.pauseButton);
              pauseButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if(isSongPlaying){
                          mp.pause();
                          pauseButton.setText(&quot;play&quot;);
                      }else{
                          mp.start();
                          pauseButton.setText(&quot;pause&quot;);
                      }
                      isSongPlaying=!isSongPlaying;
                  }
              });
  
              listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                      final String musicFilePath=musicFilesList.get(position);
                      playMusicFile(musicFilePath);
                      final int songDuration=playMusicFile(musicFilePath)/1000;
                      seekBar.setMax(songDuration);
                      playbackControls.setVisibility(View.VISIBLE);
                      seekBar.setVisibility(View.VISIBLE);
                      songDurationTextView.setText(String.valueOf(songDuration/3600)+&quot;:&quot;+String.valueOf((songDuration%3600)/60)+&quot;:&quot;+String.valueOf((songDuration%3600)%60));
                      isSongPlaying=true;
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              songPosition = 0;
                              while (songPosition &lt; songDuration) {
                                  try {
                                      Thread.sleep(1000);
                                  } catch (InterruptedException e) {
                                      e.printStackTrace();
                                  }
                                  if (isSongPlaying) {
                                      songPosition++;
                                      runOnUiThread(new Runnable() {
                                          @Override
                                          public void run() {
                                              seekBar.setProgress(songPosition);
                                              songPositionTextView.setText(String.valueOf(songPosition / 3600) + &quot;:&quot; + String.valueOf((songPosition % 3600) / 60) + &quot;:&quot; + String.valueOf((songPosition % 3600) % 60));
                                          }
                                      });
                                  }
                              }
  
                              mp.pause();
                              songPosition = 0;
                              mp.seekTo(songPosition);
                              songPositionTextView.setText(&quot;0:0:0&quot;);
                              pauseButton.setText(&quot;play&quot;);
                              isSongPlaying = false;
                              seekBar.setProgress(songPosition);
                          }
                      }
                      );
  
                  }
              });
              isMusicPLayerInit=true;
          }
      }
      class TextAdapter extends BaseAdapter{
          private List&lt;String&gt; data=new ArrayList&lt;&gt;();
          void setData(List&lt;String&gt; mdata){
              data.clear();
              data.addAll(mdata);
              notifyDataSetChanged();
          }
          @Override
          public int getCount(){
              return data.size();
          }
          @Override
          public String getItem(int position){
              return null;
          }
          @Override
          public long getItemId(int position){
              return 0;
          }
          @Override
          public View getView(int position, View convertView, ViewGroup parent){
              if(convertView==null){
                  convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
                  convertView.setTag(new ViewHolder((TextView) convertView.findViewById(R.id.myItem)));
              }
              ViewHolder holder=(ViewHolder) convertView.getTag();
              final String item=data.get(position);
              holder.info.setText(item.substring(item.lastIndexOf('/')+1));
              return convertView;
          }
      }
      class ViewHolder{
          TextView info;
          ViewHolder(TextView minfo){
              info=minfo;
          }
      }
  }
  </code></pre>
  <p>the logcat error is :</p>
  <pre><code>2020-10-28 20:23:40.437 7927-7927/? I/ple.musicplaye: Not late-enabling -Xcheck:jni (already on)
  2020-10-28 20:23:40.454 7927-7927/? E/ple.musicplaye: Unknown bits set in runtime_flags: 0x8000
  2020-10-28 20:23:40.454 7927-7927/? W/ple.musicplaye: Unexpected CPU variant for X86 using defaults: x86
  2020-10-28 20:23:40.530 7927-7927/com.example.musicplayer W/RenderThread: type=1400 audit(0.0:47): avc: denied { write } for name=&quot;property_service&quot; dev=&quot;tmpfs&quot; ino=6871 scontext=u:r:untrusted_app:s0:c144,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0
  2020-10-28 20:23:40.545 7927-7961/com.example.musicplayer D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
  2020-10-28 20:23:40.545 7927-7961/com.example.musicplayer W/libc: Unable to set property &quot;qemu.gles&quot; to &quot;1&quot;: connection failed; errno=13 (Permission denied)
  2020-10-28 20:23:40.550 7927-7961/com.example.musicplayer D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
  2020-10-28 20:23:40.552 7927-7961/com.example.musicplayer D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
  2020-10-28 20:23:40.556 7927-7961/com.example.musicplayer D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
  2020-10-28 20:23:40.592 7927-7927/com.example.musicplayer D/AndroidRuntime: Shutting down VM
  2020-10-28 20:23:40.593 7927-7927/com.example.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.musicplayer, PID: 7927
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
          at android.os.Handler.dispatchMessage(Handler.java:107)
          at android.os.Looper.loop(Looper.java:214)
          at android.app.ActivityThread.main(ActivityThread.java:7356)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
          at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
          at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
          at android.content.Context.obtainStyledAttributes(Context.java:738)
          at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
          at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
          at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
          at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
          at com.example.musicplayer.MainActivity.&lt;init&gt;(MainActivity.java:108)
          at java.lang.Class.newInstance(Native Method)
          at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
          at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
          at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
          at android.os.Handler.dispatchMessage(Handler.java:107) 
          at android.os.Looper.loop(Looper.java:214) 
          at android.app.ActivityThread.main(ActivityThread.java:7356) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
  </code></pre>
  <p>need some help please
  the item.xml file is:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/myItem&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;150dp&quot;
      android:gravity=&quot;center_vertical&quot;
      android:textSize=&quot;20sp&quot;
      android:paddingStart=&quot;16dp&quot;
      android:paddingLeft=&quot;16dp&quot;
      android:paddingEnd=&quot;16dp&quot;
      android:paddingRight=&quot;16dp&quot;
      android:textColor=&quot;#00ffff&quot;&gt;
  
  &lt;/TextView&gt;
  </code></pre>
  <p>and activity_main.xml file is :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;ListView
          android:id=&quot;@+id/listview&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_weight=&quot;1&quot; /&gt;
  
      &lt;LinearLayout
          android:id=&quot;@+id/playbackButtons&quot;
          android:visibility=&quot;gone&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;horizontal&quot; &gt;
          &lt;TextView
              android:paddingStart=&quot;16dp&quot;
              android:id=&quot;@+id/currentPosition&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_weight=&quot;1&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:paddingLeft=&quot;16dp&quot; /&gt;
          &lt;Button
              android:id=&quot;@+id/pauseButton&quot;
              android:text=&quot;pause&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_weight=&quot;1&quot;/&gt;
          &lt;TextView
              android:gravity=&quot;end&quot;
              android:paddingEnd=&quot;16dp&quot;
              android:paddingRight=&quot;16dp&quot;
              android:id=&quot;@+id/songDuration&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_weight=&quot;1&quot;
              android:layout_height=&quot;wrap_content&quot;/&gt;
      &lt;/LinearLayout&gt;
      &lt;SeekBar
          android:visibility=&quot;gone&quot;
          android:id=&quot;@+id/seekBar&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>