0

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.

13
  • private signedOutItems = new LendingItem[0]; Tada! Commented Nov 28, 2014 at 0:45
  • will this allow me to add more than one thing to the array though? Commented Nov 28, 2014 at 0:46
  • no. An array of length 0 is immutable and nothing can be added. Commented Nov 28, 2014 at 0:46
  • If you want to add elements in the future, use an ArrayList Commented Nov 28, 2014 at 0:46
  • Okay. this make another error for me because I need to test to see how many spots in the array are filled and return the contents... Commented Nov 28, 2014 at 0:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

As a side-note, arrays are awkward, especially as fields. Lists are a lot more convenient.
@maksimov Or realistically, most instances of Collection
0

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..

3 Comments

THANK YOU. this helps a bunch. basically it was like "this is how you make arrays, now do this assignment."
@NIcholasTremblay you're welcome, but it doesn't look like this is what your teacher wants either... Either way, I edited the code to be closer to what he might want.
This works, its returns the true statement and I completely understand this. For all intensive purpose this completely works without using array lists. you @yts are a gentleman and a scholar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.