java.lang.NullPointerException: Cannot print array contents -


this question has answer here:

i trying write simple banking program.

the basic functions :

1. open account - (open <amount>)-- returns account number 2.check balance - (balance <accountnumber>) 

the code following this.i not posting other classes , class contatins main(), because believe problem lies somewhere in code ::

public class bankaccount { private static int bankaccnogen=1001; private int balance; private int bankaccno; private  int i=0;  bankaccount[] bankarray=new bankaccount[10];  public void openbankaccount(int openamount){     bankaccount ba =new bankaccount();     ba.balance=openamount;     ba.bankaccno=bankaccnogen;      bankarray[i] = ba;     system.out.println("account opened account number : "+bankarray[i].bankaccno);     system.out.println("please note account number later use.");     system.out.println("balance account no : "+bankarray[i].bankaccno+" : "+ bankarray[i].balance);     ++bankaccnogen;++i;  } public void printbalance(int accno){       for(int i=0;i<10;i++){         if(bankarray[i].bankaccno==accno){             system.out.println("account number :"+ accno +" has :"+bankarray[i].balance);         }     }  } 

}

after running can open bank account :

open 1000 

output:

account opened account number : 1001 please note account number later use. balance account no : 1001 : 1000  can check balance :  balance 1001 

error:

 exception in thread "main" java.lang.nullpointerexception  @  bankaccount.printbalance(bankaccount.java:27)  @  readinput.inputread(readinput.java:36)     @  simplebank.main(simplebank.java:11) 

your bankarray contains 1 element @ [0], you're getting nullpointer on second hit, @ point: bankarray[i].balance

enter image description here


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -