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

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

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

html - jQuery UI Sortable - Remove placeholder after item is dropped -