Creating an Object array from another class

2019-09-17 02:48发布

问题:

Okay this should be simple. But I have been at this for a good hour and cannot figure out why its a nullpointerexception.

I need to create an array for "Sally" and then test its length, which should be 0. This is in the main method of the driver class.

 LendingItem[] sallysItemList = sally.getSignedOutItems();
if (sallysItemList.length == 0)

and this is in the object class that created sally. that rotten B.

    private LendingItem[] signedOutItems;

public LendingItem[] getSignedOutItems() {
    return signedOutItems;
}

I feel like I need to maybe declare the Lending item as

private LendingItem[] signedOutItems = {};

but I still get errors with that also.

EDIT:

Im going to add more so that its more understandable what I need to make happen

Provided code below.

   System.out.println("\n*** Test case #1: Create a CardHolder object & test accessors");
CardHolder sally = new CardHolder("Sally Smith",
                                  152,
                                  "454-1234");
System.out.println("Name:     " + sally.getName()
               + "\nAppt #:   " + sally.getAptNumber()
               + "\nPhone:    " + sally.getPhoneNumber()
               + "\nMember #: " + sally.getMembershipNumber());

LendingItem[] sallysItemList = sally.getSignedOutItems();
if (sallysItemList.length == 0)
  System.out.println("Correct result: Sally has zero lending items.");
else
  System.out.println(">> ERROR: Sally has more than zero lending items.");


 System.out.println("\n*** Test case #6: Sign out one LendingItem");

if(sally.signOut(testItemList[0]))
{ System.out.println("Correct result: Sally signed out an item successfully.");
  sallysItemList = sally.getSignedOutItems();
  if (sallysItemList.length == 1)
    System.out.println("Correct result: Sally has one lending item.");
  else
    System.out.println(">> ERROR: Sally has other than one lending item.");
}
else
  System.out.println(">> ERROR: Sally was unable to sign out an item.");

This is my code thus far. Just need to return the current signed out items.

    public LendingItem[] getSignedOutItems() {
    return signedOutItems;
}

This is how we are expected to add to our array, it needs to return a boolean

    public boolean signOut(LendingItem lendingItem) {
    if (signedOutItems.length < 7) {
        signedOutItems[0] = lendingItem;
        return true;
    } else {
        return false;
    }

Dont need the outright code just an idea of how to actually accomplish this.

回答1:

This is one way to make a crazy class which uses an array to maintain the list, with the ability to return arrays with different sizes.

public class CardHolder {
    private LendingItem[] lendingItems;

    public CardHolder () {
        lendingItems = new LendingItem[0];
    }

    public boolean signOut(LendingItem item) {
        if (lendingItems.length >= 7) return false;
        lendingItems = Arrays.copyOf(lendingItems, lendingItems.length + 1); //copies old array, but adding a null value to the new one
        lendingItems[lendingItems.length - 1] = item; //replace the null value with the new item to add
        return true;
    }

    public LendingItem[] getSignedOutItems() {
        return lendingItems;
    }
}

That should help get you started..



回答2:

This

private LendingItem[] signedOutItems;

is equivalent to

private LendingItem[] signedOutItems = null;

so when you do,

public LendingItem[] getSignedOutItems() {
    return signedOutItems;
}

It returns null. Initialize and add elements to your array. Something like,

private LendingItem[] signedOutItems = new LendingItem[10];

creates an array with space for 10 LendingItem(s). Note that each of those 10 slots is initialized to null.