java - Bank account program. NullPointerException error -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i working on bank account program java class. still new this, , our first assignment working arrays. please excuse rookie errors lol. assignment requires array of 3 bank accounts now. need 3 classes; bank (which contains bankacct myacct[] = new bankacct), bankuser, , bankacct(which contains withdraw, deposit , see balance methods). of getting nullpointerexception in prochoice method anytime try view balance, deposit, or withdraw.the error in user class, leave comments in code show where. please, appreciated. reading textbook not finding can particular issue. thank in advance.
bankacct class
import java.util.scanner; public class bankacct { private double bal; private int acctnum; private string name; scanner scannerobject = new scanner(system.in); public bankacct(int pacctnum, double pbal, string pname) { pbal = bal; pacctnum = acctnum; pname = name; } public void makedeposit() { system.out.print( "hello " + name + ", please, enter amount deposit $"); double ldep; ldep = scannerobject.nextdouble(); bal = bal + ldep; system.out.println( " have deposited $" + ldep); system.out.println( " new balance $" + bal); } public void makewithdrawal() { system.out.print( "hello " + name + ", please, enter amount withdraw $"); double lwdraw; lwdraw = scannerobject.nextdouble(); if (lwdraw <= bal){ bal = bal - lwdraw; system.out.println( "you have withdrawn $" + lwdraw); system.out.println( "your new balance $" + bal); }else{ system.out.println("insufficient funds!"); } } public void dispbal() { system.out.println( "your current balance $" + bal); } public void setacctnum(int pacctnum) { pacctnum = acctnum; } public int getacctnum() { return acctnum; } public void setname(string pname) { pname = name; } public string getname() { return name; } }
bank class
import java.util.scanner; public class bank { int max = 3; int count; bankacct myacct[] = new bankacct[max]; bankuser user = new bankuser(); scanner scannerobject = new scanner(system.in); public void openacct() { string lname; if (count >= max){ system.out.println("not accepting new customers @ time."); }else{ system.out.println("please enter name: "); lname = scannerobject.nextline(); myacct[count] = new bankacct(count + 1, 0, lname); count++; system.out.println("thank " + lname + ", account number is: " + count); } } public int findacct() { int lnum = -1; system.out.println("greetings, please enter account number: "); lnum = scannerobject.nextint(); for(count = 0; count < max; count++){ if (count == lnum) return count; } return lnum; } public void seebal() { int lfound = findacct(); if (lfound == -1) { system.out.println("error!"); }else{ myacct[lfound].dispbal(); } } void deposit() { int lfound = findacct(); if (lfound == -1) { system.out.println("error!"); }else{ myacct[lfound].makedeposit(); } } void withdrawal() { int lfound = findacct(); if (lfound == -1) { system.out.println("error!"); }else{ myacct[lfound].makewithdrawal(); } } }
user class
import java.util.scanner; public class bankuser { public static void main(string[] args) { bank mybank = new bank(); scanner scannerobject = new scanner(system.in); int choice; { dispmenu(); choice = getchoice(scannerobject); prochoice(choice, mybank); ***//error occurring here*** } while (choice !=0); } public static void dispmenu() { system.out.println( "|==================================|"); system.out.println( "| tony's first national bank |"); system.out.println( "|***********menu options***********|"); system.out.println( "|__________________________________|"); system.out.println( "| press 1 open new account |"); system.out.println( "| press 2 view balance |"); system.out.println( "| press 3 make deposit |"); system.out.println( "| press 4 make withdrawal |"); system.out.println( "| press 0 exit |"); system.out.println( "|__________________________________|"); system.out.println( "| please make selection now... |"); system.out.println( "|==================================|"); } static int getchoice(scanner scannerobject) { int pchoice, choice; pchoice = scannerobject.nextint(); choice = pchoice; return choice; } static void prochoice(int choice, bank mybank) { switch (choice) { case 1: mybank.openacct(); break; case 2: mybank.seebal(); //***error here*** break; case 3: mybank.deposit(); //***error here*** break; case 4: mybank.withdrawal(); //***error here*** break; case 0: system.out.println( "thank you, come again."); break; } } }
you have couple problems. solved first one, relate second , third. first issue how create accounts. current constructor is:
public bankacct(int pacctnum, double pbal, string pname) { pbal = bal; pacctnum = acctnum; pname = name; }
it should this:
public bankacct(int pacctnum, double pbal, string pname) { bal = pbal; acctnum = pacctnum; name = pname; }
here had values reversed, prevented assigning account number, last name, , balance account created.
secondly, findacct
, seebalance
methods should this:
public bankacct findacct() { bankacct mybankacct = null; system.out.println("greetings, please enter account number: "); int acctnum = scannerobject.nextint(); //make sure use myacct.length ensure don't "arrayoutofboundsindex" error. for(count = 0; count < myacct.length; count++){ mybankacct = myacct[count]; if(mybankacct.getacctnum() == acctnum){ return mybankacct; } } return mybankacct; } public void seebal() { bankacct lfound = findacct(); if (lfound == null) { system.out.println("error!"); }else{ lfound.dispbal(); } }
your biggest problem findacct
method. never found account. did:
public int findacct() { int lnum = -1; system.out.println("greetings, please enter account number: "); //you users acct # lnum = scannerobject.nextint(); for(count = 0; count < max; count++){ //here iterate, never "find" account if (count == lnum) return count; //then return "count" rather account itself. } return lnum; }
if make changes made, run through choice 2, there going errors going have deal with. if take lesson post, should able address other issues have in program. should able make deposit
, withdraw
methods seebalance
method.
here output ran
|==================================| | tony's first national bank | |***********menu options***********| |__________________________________| | press 1 open new account | | press 2 view balance | | press 3 make deposit | | press 4 make withdrawal | | press 0 exit | |__________________________________| | please make selection now... | |==================================| 1 please enter name: blaine thank blaine, account number is: 1 |==================================| | tony's first national bank | |***********menu options***********| |__________________________________| | press 1 open new account | | press 2 view balance | | press 3 make deposit | | press 4 make withdrawal | | press 0 exit | |__________________________________| | please make selection now... | |==================================| 2 greetings, please enter account number: 1 current balance $0.0 |==================================| | tony's first national bank | |***********menu options***********| |__________________________________| | press 1 open new account | | press 2 view balance | | press 3 make deposit | | press 4 make withdrawal | | press 0 exit | |__________________________________| | please make selection now... | |==================================|
i hope helps :)
Comments
Post a Comment