![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0
![]() |
Problem with code - system null ref error
V studio is saying "Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.
at ATM.Main<> in c:\.........\ATM_mod.cs:line 11" using System;
public class ATM
{
public static void Main()
{
int x = 0;
AccountData[] TransArray = new AccountData[2];
TransArray[0].SetAcctNum(123);
TransArray[0].SetPinsNum(90);
TransArray[0].SetAvailableBalance(1000);
TransArray[1].SetAcctNum(234);
TransArray[1].SetPinsNum(23);
TransArray[1].SetAvailableBalance(900);
string pinString,
accountString;
double pin,
account;
Console.Write("Please enter your account number: ");
accountString = Console.ReadLine();
account = Convert.ToDouble(accountString);
Console.Write("Please enter your pin number: ");
pinString = Console.ReadLine();
pin = Convert.ToDouble(pinString);
Console.WriteLine("----------------------------------------------------------");
<continues> |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Whilst I'm not that familiar with C#, I suspect your mistake is thinking that this line:
AccountData[] TransArray = new AccountData[2]; TransArray[0] = new AccountData(); TransArray[1] = new AccountData(); |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0
![]() |
ok, that worked.... but now I found another problem when I went to run it... a wonderful infinite loop, I will mark what the console is saying with a '*' before and after
NOTE: it is located near the bottom at the Console.WriteLine using System;
public class ATM
{
public static void Main()
{
int x = 0;
AccountData[] TransArray = new AccountData[2];
TransArray[0] = new AccountData();
TransArray[1] = new AccountData();
TransArray[0].SetAcctNum(123);
TransArray[0].SetPinsNum(90);
TransArray[0].SetAvailableBalance(1000);
TransArray[1].SetAcctNum(234);
TransArray[1].SetPinsNum(23);
TransArray[1].SetAvailableBalance(900);
string pinString,
accountString;
double pin,
account;
Console.Write("Please enter your account number: ");
accountString = Console.ReadLine();
account = Convert.ToDouble(accountString);
Console.Write("Please enter your pin number: ");
pinString = Console.ReadLine();
pin = Convert.ToDouble(pinString);
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine();
for(x = 0; x < TransArray.Length; ++x)
{
if (TransArray[x].GetAcctNum() == account && TransArray[x].GetPinsNum() == pin)
{
Console.Write("Do you want to make a transaction? Y or N...");
}
else
Console.WriteLine("Invalid account and pin please try again or cancel");
}
char response;
response = GetChar();
while( response == 'Y' || response == 'y')
{
Console.WriteLine(***"Please select a transaction. Balance, Withdrawal, or Deposit");***
switch (response)
{
case 'A':
case 'a':
BalanceMethod(TransArray[x].GetAvailableBalance());
break;
case 'B':
case 'b':
WithdrawalMethod(TransArray[x].GetAvailableBalance());
break;
case 'C':
case 'c':
DepositMethod(TransArray[x].GetAvailableBalance());
break;
case 'D':
case 'd':
CancelMethod();
break;
default:
Console.WriteLine("Invaild Selection");
break;
}
}
} |
|
|
|
|
|
#4 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
You get a single character before the beginning of the loop. If it is capital or lowercase y, it loops forever, printing "Please select a transaction" and skipping past a switch. Last I checked, capital or lowercase y does not match capital/lowercase A, B, C, or D. Perhaps you should repeat the prompt about making a transaction as well as fetch a new character each iteration. The order in which to do those two tasks and where is an excercise left to the OP.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0
![]() |
ok, I fixed the inf loop, but now I have come across a logic error..... somewhere in here, lol
I think I said I am trying to make a ATM program, where a user punches in his number and pin (works), and then makes a decision wheather to Withdraw from his account, deposit, or get a balance.... there is the problem... it is saying that the balance is zero, but in the array at the top, it is stated that it is 900 (or 1000, i forgot, lol) if you have any ideas, please let me know, here is the code: using System;
public class ATM
{
public static void Main()
{
int x = 0;
AccountData[] TransArray = new AccountData[2];
TransArray[0] = new AccountData();
TransArray[1] = new AccountData();
TransArray[0].SetAcctNum(123);
TransArray[0].SetPinsNum(90);
TransArray[0].SetAvailableBalance(1000);
TransArray[1].SetAcctNum(234);
TransArray[1].SetPinsNum(23);
TransArray[1].SetAvailableBalance(900);
string pinString,
accountString;
double pin,
account;
Console.Write("Please enter your account number: ");
accountString = Console.ReadLine();
account = Convert.ToDouble(accountString);
Console.Write("Please enter your pin number: ");
pinString = Console.ReadLine();
pin = Convert.ToDouble(pinString);
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine();
for(x = 0; x < TransArray.Length - 1; ++x)
{
if (TransArray[x].GetAcctNum() == account && TransArray[x].GetPinsNum() == pin)
{
Console.Write("Do you want to make a transaction? Y or N...");
}
else
Console.WriteLine("Invalid account and pin please try again or cancel");
}
char response;
response = GetChar();
while( response == 'Y' || response == 'y')
{
Console.WriteLine("Please select a transaction. Balance (a) or Withdrawal (b) or Deposit (c)");
response = GetChar();
switch (response)
{
case 'A':
case 'a':
BalanceMethod(TransArray[x].GetAvailableBalance());
break;
case 'B':
case 'b':
WithdrawalMethod(TransArray[x].GetAvailableBalance());
break;
case 'C':
case 'c':
DepositMethod(TransArray[x].GetAvailableBalance());
break;
case 'D':
case 'd':
CancelMethod();
break;
default:
Console.WriteLine("Invaild Selection");
break;
}
}
}
public static char GetChar()
{
char answer;
answer = Convert.ToChar(Console.ReadLine());
return answer;
}
public static void BalanceMethod(double bal)
{
Console.WriteLine("Your balance is {0}", bal);
//return BalanceMethod(double);
}
public static void WithdrawalMethod(double bal1)
{
double withdrawalAmount;
double amountWithDrawal;
Console.WriteLine("Please enter the amount you would like to withdraw?");
withdrawalAmount = Convert.ToDouble(Console.ReadLine());
amountWithDrawal = bal1 - withdrawalAmount;
Console.WriteLine("Your balance after your withdrawal is {0}", amountWithDrawal);
//return WithdrawalMethod(double);
}
public static void DepositMethod(double bal2)
{
double depositAmount,
depositTotal;
Console.WriteLine("Please enter the amount you would like to deposit.");
depositAmount = Convert.ToDouble(Console.ReadLine());
depositTotal = depositAmount + bal2;
Console.WriteLine("Your balance after the deposit is {0}", depositAmount);
//return DepositMethod(double);
}
public static double CancelMethod()
{
Console.WriteLine("Thank you and come again");
return CancelMethod();
}
}
class AccountData
{
private double pinNum,
acctNum,
AvailableBalance = 0;
public double GetPinsNum()
{
return pinNum;
}
public void SetPinsNum(double pin)
{
pinNum = pin;
}
public double GetAcctNum()
{
return acctNum;
}
public void SetAcctNum(double account)
{
acctNum = account;
}
public double GetAvailableBalance()
{
return AvailableBalance;
}
public void SetAvailableBalance(double AvailableBalance)
{
AvailableBalance = AvailableBalance;
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|