Monday 15 June 2015

oop - Getting a null point exception in Java code for Bank App -



oop - Getting a null point exception in Java code for Bank App -

hello trying test code when application seek sabotage code writing cc or ss when code asks checking or savings account, null pointer exception.this doesnt happen when seek same writing ww or dd withdraw or deposit. have create sure programme fool proof. here part of code error occuring.

static scanner sc = new scanner(system.in); public static void validator(){ boolean isvalid=false; while(isvalid==false) { system.out.println("withdraw or deposit? (w/d):"); if ((sc.hasnext("d"))||(sc.hasnext("w"))||(sc.hasnext("w"))||(sc.hasnext("d"))){ transaction = sc.next(); isvalid=true; } else { sc.nextline(); system.out.println("invalid input! type again"); continue; } system.out.println("checking or saving? (c/s):"); if((sc.hasnext("c"))||(sc.hasnext("c"))||(sc.hasnext("s"))||(sc.hasnext("s"))){ accounttype = sc.next(); isvalid=true; } else{ sc.nextline(); system.out.println("invalid input! type again"); continue; } system.out.println("amount? :"); if(sc.hasnextdouble()){ creditdebit = sc.nextdouble(); isvalid=true; } else{ sc.nextline(); system.out.println("invalid input! type again"); continue; } if(creditdebit<1000){ isvalid=true; } else{ sc.nextline(); system.out.println("input must lower $1000"); continue; } } } public static void main(string[] args) { string choice="y"; checkingaccount c= new checkingaccount(); savingsaccount s=new savingsaccount(); bankdisplay account1= new bankdisplay(); system.out.println("checking amount:"+account1.getchecking()); system.out.println("saving amount:"+account1.getsaving()); do{ try{ validator(); if(accounttype.isempty()){ accounttype=null; } if (transaction.equalsignorecase("w") && accounttype.equalsignorecase("c")){ c.withdraw(creditdebit); } else if (transaction.equalsignorecase("w") && accounttype.equalsignorecase("s")){ s.withdraw(creditdebit); } else if (transaction.equalsignorecase("d") && accounttype.equalsignorecase("c")){ c.deposit(creditdebit); } else if (transaction.equalsignorecase("d") && accounttype.equalsignorecase("s")){ s.deposit(creditdebit); } system.out.println("continue? (y/n): "); choice=sc.next(); } catch(inputmismatchexception e){ system.out.println("invalid input ! please seek again:"); sc.next(); continue; } } while(!choice.equalsignorecase("n"));

every time phone call hasnext you're consuming next token. (sc.hasnext("d"))||(sc.hasnext("w"))||(sc.hasnext("w"))||(sc.hasnext("d")) eat tokens file. number of tokens depends on how quickly, if @ all, status becomes true.

this not intend.

capture next token , test it.

java oop

No comments:

Post a Comment