的NullPointerException同时加入一个目的是阵列 [英] NullPointerException while adding an object to an array

查看:181
本文介绍了的NullPointerException同时加入一个目的是阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一些code是产生一个空指针异常,这意味着我的code试图使用它应该使用对象的空值(直出了问题该API)。我在做什么是我有一个类叫酒店,具有一个字符串值赋给变量,并创建类型客房,一个对象的数组构造函数。我知道这应该是直截了当但对我的生活我不知道这一点。

Alright so I'm having a problem with some code that is generating a null pointer exception, meaning that my code is attempting to use a null value where it should be using an object (Straight out of the API). What I'm doing is I have a class called Hotel that has a constructor which assigns a string value to a variable and creates an array of type Room, an object. I know this should be straight forward yet for the life of me I cannot figure this out.

我会试着保持code作为简约越好:

I'll try and keep the code as minimalist as possible:

构造一个对象,酒店,创建大小NUM_ROOMS的新数组

Constructor for an object, Hotel, that creates a new array of size NUM_ROOMS

这里有包括更新,我还是坚持了空指针问题很不幸的是

public Hotel(String hotelName)
{
    name = hotelName;
    theRoom = new Room[NUM_ROOMS];
}

此构造函数创建类型的客房,其参数赋值给变量数组

This constructor creates an array of type Room, whose parameters are assigned to the variables

public Room(String guestName, int partySize, int bedCountRequired,
        double ratePerDay, int daysStaying)
{
    guest = guestName;
    number = partySize;
    numBeds = bedCountRequired;
    rate = ratePerDay;
    numOfDays = daysStaying;
}

这下面的方法应该是:

1)使用提供的参数创建对象间的新实例

1) create a new instance of the object Room using the supplied parameters

2)分配roomsRented(定义为零的索引数组引用到该实例时,首先初始化code再经过数增一)

2) assign an array reference to that instance at the index roomsRented (defined as zero when the code is first initialized then increases by one after)

public void addReservation(String guestName, int partySize,
        int bedCountRequired, double ratePerDay, int daysStaying)
{
    Room roomHolder = new Room(guestName, partySize, bedCountRequired,
            ratePerDay, daysStaying);

    theRoom[roomsRented] = roomHolder;

    roomsRented++;
}

这我遇到的问题是,

theRoom[roomsRented] = roomHolder;

时,出现导致空指针异常。我也从Eclipse中收到错误的数组变量瞟着房间是尽管我在前面提到的方法,用它未被使用。这使我相信,我不是正确引用的对象。但是,我不知道为什么会引用不正确,即使我在瞟着房间代替[0],编译器仍然给我一个空指针。我看不到我的阵列结构的任何问题这对我来说意味着可能会导致空指针异常唯一的问题是,如果我不引用我的数组任何值。

is appearing to cause a null pointer exception. I am also getting an error from Eclipse that the array variable theRoom is unused despite that I use it in the aforementioned method. This leads me to believe that I am not referencing that object correctly. However, I don't know why that reference is incorrect, even if I substitute in theRoom[0], the compiler still gives me a null pointer. I don't see any problems with my array construction which to me means that the only problem that could cause a null pointer exception is if I don't reference any values in my array.

有什么想法?

在此先感谢任何捐款和道歉,如果答案是痛苦简单。

Thanks in advance for any contributions and apologies if the answer is painfully simple.

修改***
谢谢你的帮助,有没有发现该变量的问题已经解决,但每次我尝试了数组中引用对象我仍然得到一个空指针异常。下面是如何我想引用数组的例子:

EDIT*** Thank you for your help, the problem with the variable not being found has been resolved but everytime I try and reference the object in the array I still get a nullpointer exception. Here is an example of how I'm trying to reference the array:

public double getTotalRentalSales()
{
    // Iterate through the array
    for (int i = 0; i < theRoom.length; i++)
    {
        // sum the amount money received per rental
        totalRate = totalRate + theRoom[i].getRate();
    }
    // Return total
    return totalRate;
}

,其中的getRate()是从物体室返回可变速率的方法

where getRate() is a method that returns the variable rate from the object Room

totalRate = totalRate + theRoom[i].getRate();

导致在这种情况下,空指针。是否与我的对象引用的一个问题?

is causing the null pointer in this situation. Is there a problem with my object reference?

推荐答案

的问题是,在构造函数你在其范围内一个名为瞟着房间的变量,这意味着你将不会被分配价值的新客房[NUM_ROOMS正在定义]命名您的酒店类字段的瞟着房间而是构造函数中的一个变量,名为瞟着房间

The problem is that in the constructor you're defining in its scope a variable called theRoom, that means you won't be assigning the value new Room[NUM_ROOMS] to your Hotel class field named theRoom but rather to a variable inside the constructor called theRoom.

只是删除​​在构造函数的类型声明(室[]),并预期它会工作。
我也建议,今后使用这个关键字来帮助你避免这种问题。

Just remove the type declaration (Room[]) in the constructor and it will work as expected. I also suggest that in the future use the this keyword to help you avoid this kind of problems.

public Hotel(String hotelName)
{
 this.name = hotelName;
 this.theRoom = new Room[NUM_ROOMS];
}

这篇关于的NullPointerException同时加入一个目的是阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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