6 Replies - 1338 Views - Last Post: 17 March 2009 - 04:44 PM Rate Topic: -----

#1 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

stack overflow error

Posted 17 March 2009 - 04:02 PM

I have this class (out of many) that I am trying to add action listeners to. It will compile alright, but I am trying to get it to read the text from the jtextfield. I have the listener on it (i think), but when i type in the value (1 thru 6) it gives me the following error



Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)
at BankApplication$1ButtonListener.actionPerformed(BankApplication.java:51)




import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;

//This program provides an interface for the user to make us of the Bank class'
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//functionality.

public class BankApplication implements ActionListener {
	private static JTextField depositAccount;
	private static JTextField depositAmount;
	private static JPanel dpanel;
	private static JLabel options;
	private static JTextField userchoice;
	private static JFrame bye;
	private static JLabel byetext;

	
	public static void main (String[] args){
		
		  
		

		//A scanner for taking input from user
		Scanner in = new Scanner(System.in);
		//A Bank object to hold accounts and perform operations
		Bank theBank = new Bank();
		//A String for holding user input
		String choice = new String();

		//This while loop just keeps going back to the main menu until the user
		//chooses option 6, which is to exit
		JFrame bankframe = new JFrame();
		bankframe.setLayout(new FlowLayout());
		
		
		class ButtonListener implements ActionListener
		  {
			  
			 public void actionPerformed(ActionEvent e)
			 {
				 actionPerformed(e);
			 }
 
		 }
		
		
	   ButtonListener listen = new ButtonListener();
		
		

				bankframe.setSize(600, 600);
			   bankframe.setTitle("CAS BANK");
			   bankframe.setVisible(true);
				dpanel = new JPanel();
			   bankframe.add(dpanel);
			   options = new JLabel("1. Make a deposit " +
					   "2. Make a withdrawal " +
					   "3. Check an account balance " +
					   "4. Create a new account " +
					   "5. See a list of all accounts " +
					   "6. Exit the program");
			   
			   bankframe.add(options);
			   userchoice= new JTextField("Pick your option");
			   userchoice.addActionListener(listen);
			   
			   bankframe.add(userchoice);
			   
				
			   
		 while (!(choice.equals("6"))){

			//Printing the main menu
			System.out.println("1. Make a deposit");
			System.out.println("2. Make a withdrawal");
			System.out.println("3. Check an account balance");
			System.out.println("4. Create a new account");
			System.out.println("5. See a list of all accounts");
			System.out.println("6. Exit the program");
			System.out.println("Select an option, enter its number, and " +
					"press enter:");

			//Take choice input from user
			choice = in.next();

			//Make some space after the input to reduce clutter
			System.out.println("\n");

			//User chooses 1 to deposit
			if (choice.equals("1")){
				
				
				depositAccount= new JTextField("Please enter the bank account number:");
				dpanel.add(depositAccount);
				//Get account number
				System.out.println("Please enter the bank account number:");
				int accountNumber = in.nextInt();
				//Checks to make sure account exists
				if (theBank.checkForAccount(accountNumber)){
					//Get amount to deposit
					depositAmount= new JTextField("Please enter how much you want to deposit:");
					dpanel.add(depositAmount);
					System.out.println("Please enter the deposit amount using only digits and a decimal point:");
					double depositAmount = in.nextDouble();
					//Make the deposit
					theBank.makeDeposit(accountNumber, depositAmount);
					//Show updated balance
					System.out.printf("Your account balance is: $%,.2f" ,
							theBank.checkBalance(accountNumber));
				}
				//What to do if the account does not exist
				else
					System.out.println("No such account, returning to main " +
							"menu");
			}


			//User chooses 2 to withdraw
			else if (choice.equals("2")){
				//Get the account number
				System.out.println("Please enter the bank account number:");
				int accountNumber = in.nextInt();
				//Checks to make sure account exists
				if (theBank.checkForAccount(accountNumber)){
					//Get withdrawal amount
					System.out.println("Please enter the amount to withdraw using only digits and a decimal point:");
					double withdrawalAmount = in.nextDouble();
					//Make withdrawal
					theBank.makeWithdrawal(accountNumber, withdrawalAmount);
					//Show new balance
					System.out.printf("Your account balance is: $%,.2f" ,
							theBank.checkBalance(accountNumber));
				}
				//What to do if the account does not exist
				else
					System.out.println("No such account, returning to main " +
							"menu");
			}


			//User chooses 3 to check the balance of an account
			else if (choice.equals("3")){
				//Get the account number
				System.out.println("Please enter the bank account number:");
				int accountNumber = in.nextInt();
				//Checks to make sure account exists
				if (theBank.checkForAccount(accountNumber)){
					//Show the balance
					System.out.printf("Your account balance is: $%,.2f" ,
							theBank.checkBalance(accountNumber));
				}
				//What to do if the account does not exist
				else
					System.out.println("No such account, returning to main " +
							"menu");

			}
			//User chooses 4 to create a new account
			else if (choice.equals("4")){
					System.out.println("Please enter the initial balance:");
					double initialBalance = in.nextDouble();
					if (initialBalance<100){
						System.out.println("Initial deposit must be 100 or more");
					}
					else if(initialBalance>=100){
						
					
					//Create the new account and get its number
					int newAccountNumber = theBank.addAccount(initialBalance);
					//Show results
					System.out.println("New account created");
					System.out.println("Account number is: " +
							newAccountNumber);
					System.out.printf("Balance is: $%,.2f" ,
							theBank.checkBalance(newAccountNumber));}
			}


			//User chooses five to print table of accounts
			else if (choice.equals("5")){

				//Get the list of accounts
				String[][] accountList = theBank.getAccountList();

				//Making the frame object
				JFrame frame = new JFrame();
				//Making the frame of appropriate size to hold all accounts
				//410 Wide to hold the two 200 wide columns and the 5 wide
				//border
				//Minimum 60 tall, add 25 to make room for each account
				frame.setSize(410, 60 + (25 * (accountList.length)));
				frame.setTitle("Account List");
				//This makes it so that the frame is disposed of when the user
				//closes it, which allows the program to exit when the user
				//chooses option 6 (exit). Without this, the program keeps
				//runing because the frame is still hiding somewhere.
				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

				//Make a new account table
				AccountTableComponent accountTable = new
						AccountTableComponent(accountList);
				//Add it to the frame
				frame.add(accountTable);
				//Show the frame
				frame.setVisible(true);
			}


			//User chooses six to exit
			else if (choice.equals("6")){
				bye=new JFrame("Thank you");
				bye.setSize(200,200);
				bye.setVisible(true);
				byetext = new JLabel("Thank you for using CAS Bank");
				bye.add(byetext);
				bye.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
			}

			//If they enter anything else, it is an invalid choice, start over
			else {
				System.out.println("Invalid option, please try again:");
			}

			//Make some space before the main menu so the screen looks cleaner
			System.out.print("\n\n");
		}
	}

	public void actionPerformed(ActionEvent ae) { 
	  System.out.println(userchoice.getText()); 
	  
	} 
}



Is This A Good Question/Topic? 0
  • +

Replies To: stack overflow error

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: stack overflow error

Posted 17 March 2009 - 04:08 PM

Looks like you have created a class within your main method. I always prefere adding ActionListeners to my class like so
back = new JButton("GO BACK");
		  back.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				back_actionPerformed(); 
 			} 
  
 		}); 


And then at the bottom of your class, just provide a back_actionPerformed(); method.
Was This Post Helpful? 0
  • +
  • -

#3 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: stack overflow error

Posted 17 March 2009 - 04:15 PM

Inside of the actionlistener class

private void userchoice_actionPerformed() {
int userpicked = Double.parseDouble(userchoice);
if (userpicked==1){

}
}


I have that.
I am trying to parse the string from the textfield into a double value

But i get this error


C:\Users\hidden\Documents\NetBeansProjects\BankFinalExam\src\BankApplication.java:75: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (javax.swing.JTextField)
int userpicked = Double.parseDouble(userchoice);
1 error
BUILD FAILED (total time: 0 seconds)
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: stack overflow error

Posted 17 March 2009 - 04:21 PM

to get text out of a testField you use the getText method. Also, your parsing it as a double but trying to assign it to an int. If you need an int, you would pass it as an int. Everything from a JTextField is a String originally.
So..
int userpicked = Integer.parseInt(userchoice.getText());

Was This Post Helpful? 0
  • +
  • -

#5 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: stack overflow error

Posted 17 March 2009 - 04:27 PM

View Postnick2price, on 17 Mar, 2009 - 03:21 PM, said:

to get text out of a testField you use the getText method. Also, your parsing it as a double but trying to assign it to an int. If you need an int, you would pass it as an int. Everything from a JTextField is a String originally.
So..
int userpicked = Integer.parseInt(userchoice.getText());



Yea I got that right as you replied.

I am now setting it up to do various actions depending on what is chosen.

I have
private void userchoice_actionPerformed() {
				
				int userpicked = Integer.parseInt(userchoice.getText());
				if (userpicked==1){
					
				}
					
				}


I am wanting it to do the actions that I have in the other part of the code (see original post)
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: stack overflow error

Posted 17 March 2009 - 04:37 PM

Sorry, didnt properly see your original code. Its a bit strange what you are doing, mixing text user interface with gui. Really you should use one or the other. So your display depends on the user choice. This means when they enter a number you should have a submit button to click, then its easy to just place a listener on the button. Without the button, it will be more difficult
Was This Post Helpful? 0
  • +
  • -

#7 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: stack overflow error

Posted 17 March 2009 - 04:44 PM

View Postnick2price, on 17 Mar, 2009 - 03:37 PM, said:

Sorry, didnt properly see your original code. Its a bit strange what you are doing, mixing text user interface with gui. Really you should use one or the other. So your display depends on the user choice. This means when they enter a number you should have a submit button to click, then its easy to just place a listener on the button. Without the button, it will be more difficult

I am trying to change it from text interface to graphical. so i will be getting rid of the text as i go along. I am just trying to get the same effects when the graphical takes place
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1