Android - 在阅读文件时获取Nullpointer

时间:2017-05-19 14:47:06

标签: android nullpointerexception

我正在寻找几个星期为什么我得到一个Nullpointer并且没有找到原因。下面是我读取文件然后在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;
}

我有这个类,我实现了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;
        }
    }
}

Nullpointerstack就是这样:

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)

是否因为List(文件)为空?但我用方法“getAllNotices()”初始化它。那为什么它有时会为空呢?

额外信息: 在我的手机上它完美运行,但在我朋友的手机上它有时会抛出异常(Galaxy A3)

1 个答案:

答案 0 :(得分:0)

我不确定这是否能回答你的问题。但我注意到,如果文件夹存在且文件夹无法创建目录,则不返回 folder.exists() && !folder.mkdirs()条件未得到处理。原因可能是您的应用程序没有足够的权限来读取该目录。

您当前的逻辑是 - 如果folder doesn't exist,请检查folder cannot make directories。这意味着您假设folder does exist然后I can make directories在某些情况下可能无效。

如果抽象路径名不表示目录,或者发生I / O错误,并且如果存在安全管理器并且File.ListFiles() SecurityException,则方法SecurityManager.checkRead(String)可以返回null。 1}}方法拒绝对目录的读访问。
More on File.ListFiles

如果有帮助,请告诉我。

相关问题