数组空指针异常? [英] Array NullPointerException?

查看:31
本文介绍了数组空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我不明白为什么我的代码会报告 NullPointerException,但我已经在这里尝试了很长时间了.

Ok. I don't get why my code is reporting a NullPointerException, but I've been here for quite a while trying to figure it out.

不,files 不是 null.

这是我的一个错误,它是空的(我没有看到我运行函数的顺序.

这是我的代码:

public String[] files;

private void updateRefs(String filepath) {
    try { 
        scr++;
        files[scr] = filepath; //NullPointerException???
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

推荐答案

必须要new,否则为null:

You have to new it, otherwise it is null:

String[] files = new String[10]; //or whatever size you need

这篇关于数组空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆