I am looking for weeks now why I get a Nullpointer and didn't find yet why. Below are the code parts where I read the files and then list them in a ListView.
SplashActivity:
public static void setFolderPath() {
String dest;
try {
dest = pref.getString(FOLDER_DEST_SETTING_KEY, FOLDER_INTERNAL_DEST);
} catch (Exception e) {
dest = FOLDER_INTERNAL_DEST;
}
if (dest.equals(FOLDER_EXTERNAL_DEST) && hasExternal) {
folderPath = externalPath;
} else {
folderPath = internalPath;
SharedPreferences.Editor editor = pref.edit();
editor.putString(FOLDER_DEST_SETTING_KEY, FOLDER_INTERNAL_DEST);
editor.apply();
}
}
public static String getFolderPath() {
setFolderPath();
return folderPath == null ? internalPath : folderPath;
}
MainActivity:
/**
* Initialisierung
*/
private void init() {
initFiles();
if (listView != null) {
listView.setAdapter(new CustomSingleSelectAdapter(context, files));
listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
itemClicked(position);
}
});
} else {
Toast.makeText(context, R.string.init_fehler, Toast.LENGTH_SHORT).show();
finish();
}
ESortBy sort = ESortBy.getSorting(pref.getInt(Constants.SORT_BY_KEY, ESortBy.NOT_SORTED.ordinal()));
NotizFile.setSort(sort);
Collections.sort(files);
}
private void initFiles() {
files = Util.getAllNotices(context);
}
Util:
public static List<NotizFile> getAllNotices(Context context) {
String folderPath = SplashActivity.getFolderPath();
File folder = new File(folderPath);
if (!folder.exists()) {
if (!folder.mkdirs()) {
Toast.makeText(context, R.string.ordner_erstellen_fehler, Toast.LENGTH_SHORT).show();
return new ArrayList<>();
}
}
List<NotizFile> list = new ArrayList<>();
for (File file : folder.listFiles()) {
list.add(new NotizFile(file.getAbsolutePath(), false));
}
return list;
}
I have this class where I implement the Comparable Interface:
public class NotizFile implements Serializable, Comparable<NotizFile> {
@Override
public int compareTo(@NonNull NotizFile otherFile) {
switch (sortBy) {
case NAME_ASC:
return compareName(sortBy, this, otherFile);
case NAME_DESC:
return compareName(sortBy, this, otherFile);
case DATE_ASC:
return compareDate(sortBy, this, otherFile);
case DATE_DESC:
return compareDate(sortBy, this, otherFile);
case WICHTIG_ASC:
return compareImportance(sortBy, this, otherFile);
case WICHTIG_DESC:
return compareImportance(sortBy, this, otherFile);
default:
return 0;
}
}
}
The Nullpointerstack is this:
Caused by: java.lang.NullPointerException:
at java.util.Collections.sort(Collections.java:1869)
at daniel.com.notizapp.core.MainActivity.initFiles(MainActivity.java:117)
at daniel.com.notizapp.core.MainActivity.init(MainActivity.java:75)
at daniel.com.notizapp.core.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
Is it becaus the List (files) is null? But I am initialising it with the method "getAllNotices()". Then why is it sometimes null?
Extra Information: On my phone it works perfectly, but on the phone of my friend it sometimes throws an Exception (Galaxy A3)