创建 OBJECT ARRAYS 并使用它们时出现 NullPointerException [英] NullPointerException when creating OBJECT ARRAYS and using them

查看:57
本文介绍了创建 OBJECT ARRAYS 并使用它们时出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,在处理对象数组时遇到了问题.我的主程序是这样的:

I am new in java and I've got a problem while working with arrays of objects. My main program is like this:

package bicycledemo;

class BicycleDemo {
    public static void main(String[] args) {

        // Create two different 
        // Bicycle objects with an array
        Bicycle[] bike = new Bicycle[2];

        bike[0].cadence=50; //line 10, where the NullPointerException prompts out
        bike[0].gear=2;

        bike[1].cadence=10;
        bike[1].gear=3;

        System.out.println("gear: "+bike[0].gear+"\n"+"cadence: "+bike[0].cadence+"\n");
        System.out.println("gear: "+bike[1].gear+"\n"+"cadence: "+bike[1].cadence+"\n");


        System.out.println("\b");
    }
}

自行车类就是这个:

package bicycledemo;

public class Bicycle {
    public Bicycle() {

    }

    public int cadence;
    public int gear;

}

当我运行程序时,输出错误是:

When I run the program, the output error is:

Exception in thread "main" java.lang.NullPointerException
        at bicycledemo.BicycleDemo.main(BicycleDemo.java:10)
Java Result: 1

我想会发生什么是对象自行车没有正确创建,但我不明白为什么.

I suppose that what happens is that the object bike is not correctly created, but I don't see why.

非常感谢您的帮助!我真的很想解决这个问题!

Thank you very much for your help! I am really desperate to resolve this!

推荐答案

这一行:

Bicycle[] bike = new Bicycle[2];

创建一个数组.它有两个元素,两个元素最初都是空引用.您尚未创建任何 Bicycle 对象.然后在下一个语句中:

creates an array. It has two elements, and both elements are initially null references. You haven't created any Bicycle objects. Then in the next statement:

bike[0].cadence=50;

...您正在尝试取消引用该空值.在将数组元素用作对象之前,您需要对其进行初始化:

... you're trying to dereference that null value. You need to initialize the elements of the array before you use them as objects:

// Set the first element of the array to refer to a newly created object
bike[0] = new Bicycle();
// Then this will be fine
bike[0].cadence = 50;

如果您不完全了解这里发生了什么,那么更仔细地研究它真的很重要.理解引用和对象之间的区别(并理解变量和数组只永远包含引用或原始值,从不对象)是 Java 的基础.在你得到"之前,你会遇到各种各样的困难.

If you don't understand exactly what's going on here, it's really important that you study it more closely. Understanding the difference between references and objects (and understanding that variables and arrays only ever contain references or primitive values, never objects) is fundamental to Java. Until you "get" that, you'll run into all kinds of difficulties.

请注意,您还可以更改 Bicycle 类以包含一个以节奏和齿轮为参数的构造函数:

Note that you could also change your Bicycle class to include a constructor taking the cadence and gear as parameters:

public Bicycle(int cadence, int gear) {
    this.cadence = cadence;
    this.gear = gear;
}

然后您可以将初始化更改为:

Then you could change your initialization into:

bike[0] = new Bicycle(50, 2);
bike[1] = new Bicycle(10, 3);

或者甚至作为数组创建的一部分:

Or even as part of the array creation:

Bicycle[] bike = {
    new Bicycle(50, 2),
    new Bicycle(10, 3)
};

这篇关于创建 OBJECT ARRAYS 并使用它们时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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