Any Java Gurus want to help me

This is the place to discuss the latest computer hardware issues and technology. Please keep the discussion ON TOPIC!
Post Reply
User avatar
dicecca112
Site Admin
Site Admin
Posts: 5014
Joined: Mon Mar 01, 2004 10:40 am
Contact:

Any Java Gurus want to help me

Post by dicecca112 »

I get a null pointer exception. The Create Account works, and the Quit Sorta works, but not the process transaction. Sometimes 1 transaction will sorta work, meaning it trips the transaction++, but othertimes it won't

Code: Select all

//	SavingsAccount.java

	import java.awt.*;				//Container, FlowLayout
	import java.awt.event.*;		//ActionEvent, ActionListener

	import javax.swing.*;			//JApplet, JButton, JLabel, JTextField


	public class SavingsAccount extends JApplet implements ActionListener	{

//	variables for GUI

	String name;				//variable to hold account name from accountNameField
	float startingBalance;  //variable to hold account balance from sBalanceField
	float currentBalance;		//variable to hold account balance from cBalanceField
	float transactionAmount;//variable to hold transaction amount from transactionfield
	static int transaction = 0;	//variable to hold # of transactions

//	graphical user interface components
	//labels to identify fields
	JLabel accountName, sBalance, transaction1, cBalance;

	//strings for the labels
	private static String accountNameString = "Enter Account Name";
	private static String startingBalanceString = "Enter Starting Balance";
	private static String transactionAmountString = "Enter Transaction Amount";
	private static String currentBalanceString = "Current Balance";

	JTextField accountNameField, sBalanceField, transactionField, cBalanceField;
	JButton createAccountButton, processTransactionButton, quitButton;

//	set up GUI components

	public void init()
	{
		//obtain content pane and change its layout to FlowLayout
		Container container = getContentPane();
		container.setLayout( new FlowLayout());

		//create label and text field for Account Name
		accountName = new JLabel (accountNameString);
		container.add( accountName );
		accountNameField = new JTextField ( 10 );
		accountNameField.setEditable( true );
		container.add( accountNameField );


		//create label and text field for Starting Balance
		sBalance = new JLabel (startingBalanceString);
		container.add( sBalance );
		sBalanceField = new JTextField ( 10 );
		sBalanceField.setEditable( true );
		container.add( sBalanceField );

		//create label and text field for Transaction Amount
		transaction1 = new JLabel (transactionAmountString);
		container.add( transaction1 );
		transactionField = new JTextField ( 10 );
		transactionField.setEditable( true );
		container.add( transactionField );

		//create label and text field for Current Balance
		cBalance = new JLabel (currentBalanceString);
		container.add( cBalance );
		cBalanceField = new JTextField ( 10 );
		cBalanceField.setEditable( false );
		container.add( cBalanceField );

		//create button user clicks to create account
		createAccountButton = new JButton( "Create Account" );
		createAccountButton.addActionListener( this );
		container.add( createAccountButton );

		//create button user clicks to process tranaction
		processTransactionButton = new JButton( "Process Transaction" );
		processTransactionButton.addActionListener( this );
		container.add( processTransactionButton );

		//create button user clicks to quit
		quitButton = new JButton( "Quit" );
		quitButton.addActionListener( this );
		container.add( quitButton );

	}//end method init


//process button clicks

	public void actionPerformed( ActionEvent e )
	{
		if (e.getSource() == quitButton)
		{
			quit();
		}
		else if(e.getSource() == processTransactionButton)
			{
				processTransaction();
			}
		else if(e.getSource() ==createAccountButton)
			{
				createAccount();
			}

		//here is where the three actions should be implemented for Create and Process and Quit

	}//end method actionPerformed


	// keeps track of transactions performed and protects agains Overdrafts
	public void processTransaction()
	{

		if (currentBalance < 0 )
		{
			showStatus( "OverDraft, please try again" );
		}
		else if (currentBalance > 0)
		{
			currentBalance = startingBalance + transactionAmount;
			cBalanceField.setText(Float.toString(currentBalance));

			SavingsAccount.transaction++;
		}

		transactionField.setText("");


	}// end method processTransaction

	//method to Create Account
	public void createAccount()
	{
		name = accountNameField.getText();

		String newStartingBalance = Float.toString(startingBalance) ;
		newStartingBalance = sBalanceField.getText();

		accountNameField.setText(name);
		sBalanceField.setText(newStartingBalance);
		
		accountNameField.setEditable(false);
		sBalanceField.setEditable(false);
		

		showStatus( "Account Created" );

	}	// end method createAccount()

	public void quit()
	{

		showStatus( "Bye " + name + " you had " + transaction + " transactions" );

		//need to figure out how to add variables in output string

		accountNameField.setText("");
		sBalanceField.setText("");
		cBalanceField.setText("");
		accountNameField.setEditable(true);
		sBalanceField.setEditable(true);
	}

	}//end class SavingsAccount
Image
Post Reply