NullPoInterException:尝试在Android Studio中访问资产中的文件夹时尝试获得空数组的长度

发布于 2025-01-17 17:12:26 字数 987 浏览 1 评论 0原文

在我的 android 项目中,我在资产中添加了一个名为 validator 的文件夹。 validator 文件夹内有一些 json 文件。 我想访问它们,然后使用管理这些文件

File[] files = f.listFiles

当我运行应用程序时,出现以下错误:

java.lang.NullPointerException:尝试获取空数组的长度 引起原因:java.lang.NullPointerException:尝试获取长度 空数组

我假设这里的问题是我提供的路径。项目结构如下:

MyApplication    
│
└───app
    │
    └───src
        │
        └───main
            │
            └───res
            │
            └───assets
            │
            └───java
                │
                └───Main activity

因此,在主活动中,我创建了一个包含字符串的路径,然后

    File f = new File(path); 
    File[] files = f.listFiles

但结果是一个错误,指出数组为空。 我提供的路径是:

String path = "..\\MyApplication\\app\\src\\main\\assets";

最后,我在清单中添加了有关外部存储的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

路径是否不正确或者是否存在其他问题? 先感谢您!

in my android project I have added a folder inside assets called validator. Inside validator folder there are some json files.
I want to access them and then manage these files with

File[] files = f.listFiles

When I am running the application I get the following error:

java.lang.NullPointerException: Attempt to get length of null array
Caused by: java.lang.NullPointerException: Attempt to get length of
null array

I am assuming that the issue here is the path I am providing. The project structure is the following:

MyApplication    
│
└───app
    │
    └───src
        │
        └───main
            │
            └───res
            │
            └───assets
            │
            └───java
                │
                └───Main activity

So, inside main activity I have created a path containing the string and then

    File f = new File(path); 
    File[] files = f.listFiles

But the result is an error saying that the array is null.
The path I have provided is :

String path = "..\\MyApplication\\app\\src\\main\\assets";

Finally, I have added the permission in the manifest regarding the external storage

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Is the path incorrect or is there another issue?
Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文