0

Hi for my program I trying to make an array bigger by creating another array to copy the array to and then returning those values back to the original array.

import java.util.Scanner;
import java.util.Arrays;

public class Bank {

    public static void main(String[] args) {

        Scanner command = new Scanner(System.in);

        BankAccount [] arrayAcc = new BankAccount[1];
        BankAccount [] arrayAcc1 = new BankAccount[arrayAcc.length + 1];

        for(int z = 0; z < arrayAcc.length; z++){
            arrayAcc[z] = arrayAcc1[z];
        }
        arrayAcc = arrayAcc1;

        System.out.println("Enter new account number: ");
        int accNum = command.nextInt();
        arrayAcc[count++].setAccountID(accNum);
    }
}

When I later try to get an object from this array it is giving me an error:

Exception in thread "main" java.lang.NullPointerException
at Bank.main(Bank.java:47)
3
  • Post the relevant code to replicate the problem. Seems like your problem is about reinitializing a non-field variable. Commented Jul 14, 2014 at 0:43
  • @LuiggiMendoza I put all of my code for the program up. Commented Jul 14, 2014 at 0:50
  • @Victor exactly the comment's point. Just post the relevant code, so viewers don't have to read through all of them. Commented Jul 14, 2014 at 1:13

2 Answers 2

2

You copy items from arrayAcc1 to arrayAcc while should be conversely.

Should be:

arrayAcc1[z] = arrayAcc[z]; 

BTW, why not to use List<BankAccount> accs = new ArrayList<BankAccounts>()?

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

4 Comments

thanks for catching that! @Keenle what would 'List<BankAccount> accs = new ArrayList<BankAccounts>()' do?
List<T> automatically resizes as you add items. So you can just call accs.add(new BankAccount()) and no need to deal with array resizing routine.
@keenle I tried to do that but how would I add a new object to the array?
You can remove BankAccount[] array and create accs ArrayList<BankAccount> and work with it instead of dealing with array routines.
0

You don't need the for loop, because Arrays.copyOf already does the work for you. You also don't need the cast, because the method returns an array of the same type as its argument.

The NullPointerException is thrown when you try to access an array index that has not had a value assigned to it. You haven't filled your array with any BankAccount objects, so when you execute arrayAcc[count++].setAccountID(accNum);, there is not a BankAccount to set the account ID of. To add an account to the list, call the constructor after the array has been resized, and add the object to the last index. The code should be similar to this:

BankAccount []arrayAcc1 = Arrays.copyOf(arrayAcc, arrayAcc.length + 1);
arrayAcc = arrayAcc1;
arrayAcc[arrayAcc.length - 1] = new BankAccount(/*arguments here*/);

1 Comment

Thank you! You caught my problem!

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.