NullPointerException异常创建对象的数组时, [英] NullPointerException when Creating an Array of objects

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

问题描述

我一直在试图创建一个包含两个值的类的数组,但是当我尝试将值应用于数组我得到一个NullPointerException异常。

I have been trying to create an array of a class containing two values but when I try to apply a value to the array I get a NullPointerException.

public class ResultList {
    public String name;
    public Object value;

    public ResultList() {}
}

public class Test {
    public static void main(String[] args){
        ResultList[] boll = new ResultList[5];
        boll[0].name = "iiii";
    }
}

为什么会出现这个错误,我该如何解决这个问题?

Why am I getting this error and how can I fix it?

推荐答案

您创建数组,但没有放任何东西在里面,所以你有一个包含5个元素,所有这一切都是空的数组。你可以添加

You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add

boll[0] = new ResultList();

你设置铃前行[0]。名称。

before the line where you set boll[0].name.

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

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