nullpointerexception - Null Pointer Exception on Array in Java -
the following code returning nullpointerexception in java. can clarify mistake?
public void deposit2() { bankaccounts[] accounts2 = new bankaccounts[10]; accounts2[3].deposit(); }
bankaccounts[] accounts2 = new bankaccounts[10];
is same as
bankaccounts[] accounts2 = {null, null, null, ... null }; // (10 times)
you need assign values elements of accounts2
(or, @ least element 3) before attempt dereference them.
Comments
Post a Comment