Sunday, 15 March 2015

c# - NullReferenceException Unhandled (Cannot find the source of the error) -



c# - NullReferenceException Unhandled (Cannot find the source of the error) -

my problem i'm trying create application, , far can see, should pristine , cannot find error @ all.

here code below. comment error hits. form code

namespace techbank { public partial class tech_bank : form { caccount currentaccount = null; cbank mybank = new cbank(); private void displaybalance() { if (lstaccounts.items.count != 0) { txtbalance.text = currentaccount.balance.tostring; //where error hits txtcustomer.text = currentaccount.customername; txtaccounttype.text = convert.tostring(currentaccount.accounttype); } } private void button1_click(object sender, eventargs e) { open_account form = new open_account(); form.showdialog(); if (form.dialogresult == dialogresult.ok) { currentaccount = new caccount(typeaccount.checking, "", 4); if (form.rbtchequing.checked) currentaccount.accounttype = typeaccount.checking; if (form.rbtsavings.checked) currentaccount.accounttype = typeaccount.savings; seek { currentaccount.balance = convert.todouble(form.txtstartingbalance.text); } grab (formatexception) { messagebox.show("please come in valid information", "error in business relationship creation, please double check values correct"); } currentaccount.customername = form.txtcustomername.text; mybank.openaccount(currentaccount); lstaccounts.items.add(currentaccount.accountid); currentaccount = mybank.getaccount(lstaccounts.selectedindex); lstaccounts.selectedindex = lstaccounts.items.count - 1; displaybalance(); } } private void button2_click(object sender, eventargs e) { transaction form = new transaction(currentaccount); form.showdialog(); if (form.dialogresult == dialogresult.ok) { } displaybalance(); } private void button3_click(object sender, eventargs e) { if (lstaccounts.items.count != 0) { mybank.closeaccount(currentaccount); lstaccounts.items.removeat(lstaccounts.selectedindex); txtaccounttype.clear(); txtbalance.clear(); txtcustomer.clear(); } } private void btnexit_click(object sender, eventargs e) { system.environment.exit(0); } private void lstaccounts_selectedindexchanged(object sender, eventargs e) { currentaccount = mybank.getaccount(lstaccounts.selectedindex); displaybalance(); } } }

caccount.cs code below

namespace techbank { public enum typeaccount { checking, savings } public class caccount { private static random randomnumber = new random(); private typeaccount maccounttype; private double mbalance; private string mcustomer; private string mid; public caccount(typeaccount newtype, string newcustomer, double newbalance) { maccounttype = newtype; mcustomer = newcustomer; mbalance = newbalance; mid = convert.tostring(randomnumber.next(1, 9999)); } public typeaccount accounttype { { homecoming maccounttype; } set { maccounttype = value; } } public double balance { { homecoming mbalance; } set { mbalance = value; } } public string customername { { homecoming mcustomer; } set { mcustomer = value; } } public string accountid { { homecoming mid; } } public void deposit(double amount) { if (ispositivenumber(amount, 0)) mbalance += amount; } public bool ispositivenumber(double larger, double smaller) { homecoming (larger >= smaller); } public void withdraw(double amount) { if (ispositivenumber(mbalance, amount)) mbalance -= amount; } } }

please inform me if need more code.

null reference exceptions mean same thing: haven't initialized variable. in case, declare caccount currentaccount = null; class member. if need non-null, needs initialized calling new caccount() time before displaybalance() called. example, if user clicks button2 before clicking button1 you'll null ref. similarly, if mybank.getaccount() returns null you'll null ref. stack trace help narrow downwards of these cause.

c# .net winforms nullreferenceexception

No comments:

Post a Comment