I am making a file manager app in android studio, everything seems to work but when I open the app in the emulator it closes and says: "App keeps stopping". The logcat is giving an error like this: x
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:283)
at com.roboproffa.filemanager.FileListActivity.onCreate(FileListActivity.java:33)
at android.app.Activity.performCreate(Activity.java:8290)
at android.app.Activity.performCreate(Activity.java:8269)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657)
I think the error is on line 32 or 33 of this code:
package com.roboproffa.filemanager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class FileListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_list);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
TextView noFiles = (TextView) findViewById(R.id.noFiles);
String FilePath = getIntent().getStringExtra("path");
File root;
root = new File(FilePath);
File[] filesAndFolders = root.listFiles();
if (filesAndFolders == null || filesAndFolders.length == 0) {
noFiles.setVisibility(View.VISIBLE);
return;
}
noFiles.setVisibility(View.INVISIBLE);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(getApplicationContext(), filesAndFolders));
if(checkPermission()) {
//permission allowed
Intent intent = new Intent(FileListActivity.this, FileListActivity.class);
String path = Environment.getExternalStorageDirectory().getPath();
intent.putExtra("path", path);
startActivity(intent);
}else {
//permission not allowed
requestPermission();
}
}
//permission to access
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(FileListActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(result == PackageManager.PERMISSION_GRANTED){
return true;
}else{
return false;
}
}
private void requestPermission() {
if(ActivityCompat.shouldShowRequestPermissionRationale(FileListActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(FileListActivity.this, "Storage permission is required, please allow in settings", Toast.LENGTH_SHORT).show();
}
ActivityCompat.requestPermissions(FileListActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 111);
}
}
can someone please help, I googled it but I haven't found the solution.
CodePudding user response:
Replace the code
File root;
root = new File(FilePath);
with the single line
File root = new File(FilePath);
The mistake is that you are trying to first initialise the file and then set the path on it.
Also, try to keep Java naming conventions for variables, so you could replace FilePath
with filePath
CodePudding user response:
As you can see my screenshot, the cause is your "FilePath" is null.
at java.io.File.<init>(File.java:283)
Please check String FilePath = getIntent().getStringExtra("path");
should not be null.