Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 4:31 PM   #1
absentmindedjwc
Newbie
 
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0 absentmindedjwc is on a distinguished road
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>
absentmindedjwc is offline   Reply With Quote
Old Feb 15th, 2006, 4:56 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Whilst I'm not that familiar with C#, I suspect your mistake is thinking that this line:
AccountData[] TransArray = new AccountData[2];
Will make an array of two AccountData objects. I'm fairly sure it won't. It'll make an array capable of holding two AccountData objects, but you'll have to create them yourself:
TransArray[0] = new AccountData();
TransArray[1] = new AccountData();
Arevos is offline   Reply With Quote
Old Feb 15th, 2006, 6:22 PM   #3
absentmindedjwc
Newbie
 
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0 absentmindedjwc is on a distinguished road
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;
			}
		}
	}
absentmindedjwc is offline   Reply With Quote
Old Feb 15th, 2006, 6:41 PM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Feb 17th, 2006, 12:50 PM   #5
absentmindedjwc
Newbie
 
Join Date: Feb 2006
Location: Near Chicago
Posts: 5
Rep Power: 0 absentmindedjwc is on a distinguished road
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;
	}
}
absentmindedjwc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC