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

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -