Results 1 to 2 of 2

Thread: JTextArea error

  1. #1
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default JTextArea error

    I'm expecting more than 3 lines of text on JTextArea when i execute it with JOptionPane.showMessageDialog.

    But when i try to loop more than 1 time it shows error message.
    What's the matter with my loop?

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at checkingAccount.getTrans(
    at sample13.main(

    Java Code:
    import java.awt.Font;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    public class sample13 {
    	private static ArrayList<Transaction> list;
    	public static void main(String[] args) {
    		checkingAccount check = new checkingAccount(1000);
    		Transaction newTrans = new Transaction(1,1,2000);
    		newTrans = new Transaction(2,2,3000);
    		newTrans = new Transaction(3,1,4000);
    		int index;
    		double amount = 0;
    		int nums = 0;
    		int types = 0;
    		String line;
    		line = String.format("List All Transactions" + "\n" + "ID        Type       Amount\n" );
    		JTextArea text = new JTextArea(line);
    		for(index = 0 ;index < 2; index++){
    			types = check.getTrans(index).getTransId();
    			nums = check.getTrans(index).getTransNumber();
    			amount = check.getTrans(index).getTransAmount();
    			line = String.format("%-10s  %3d  %10s", nums,types,amount);
    		text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    		JOptionPane.showMessageDialog(null, text);
    Java Code:
    import java.util.ArrayList;
    public class checkingAccount{
    	private double balance = 0;
    	private double totalServiceCharge = 0;
    	private ArrayList<Transaction> transList;  // keeps a list of Transaction objects for the account
    	private int transCount = 0;   // the count of Transaction objects and used as the ID for each transaction 
    	private int transSize = 0;
    	public checkingAccount(double initialBalance){
    		balance = initialBalance;
    	public double getBalance(){
    		return balance; 
    	public void setBalance(double transAmt, int tCode){
    		if(tCode == 1)
    			balance -= transAmt;
    		else //if(tCode == 2)
    			balance += transAmt; 
    	public double getServiceCharge(){    
    		return totalServiceCharge;
    	public void setServiceCharge(double currentServiceCharge){
    		totalServiceCharge += currentServiceCharge;  
    	public void addTrans(Transaction newTrans){// adds a transaction object to the transList
    		transList = new ArrayList<Transaction>();
    	public int gettransCount(){
    		return transCount;  //returns the current value of transCount;
    	public Transaction getTrans(int i){
    		return transList.get(i); // returns the i-th Transaction object in the list
    	public int getSize(){
    		transSize = transList.size();
    		return transSize;
    Java Code:
    public class Transaction{
        private int transNumber = 0; 
        private int transId = 0;
        private double transAmt = 0;
        public Transaction(int number, int id, double amount){
            transNumber = number;
            transId = id;
            transAmt = amount;
        public int getTransNumber(){
            return transNumber;
        public int getTransId(){
            return transId;
        public double getTransAmount(){
            return transAmt;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: JTextArea error

    I am not able to run your code, but this worries me:

    Java Code:
        public void addTrans(Transaction newTrans){// adds a transaction object to the transList
            transList = new ArrayList<Transaction>();
    You appear to be trying to make a new ArrayList for transList each time you wish to add a new Transaction to the transList, and I don't think that you really want to do that since this will discard any Transcations that have been added to the previous transList ArrayList. Instead, why not create the ArrayList in the class's constructor, and then in the addTrans method, simply add a Transaction to it?

Similar Threads

  1. JTextArea
    By larry_d1990 in forum Advanced Java
    Replies: 5
    Last Post: 03-11-2011, 06:08 PM
  2. JTextArea
    By johnny157 in forum New To Java
    Replies: 2
    Last Post: 03-03-2011, 10:52 PM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 08:51 PM
  4. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 08:27 PM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 02:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts