Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Does not create a Transaction inside the array. Gui, objects, and arraylist

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Does not create a Transaction inside the array. Gui, objects, and arraylist

    My problem is that I create a panel asking the balance first. Panel works good but when I click on make a transaction and input transcode and amount I get an error on the following lines.

    There is a problem and gives errors like abstract button, defaultbutton, JToggle and others.
    I don't know if it has to do with the order of how it organized it worked yesterday but Listing the transactions was giving me problems after a few changes and saves computer crashed and know I can't remember what I changed so the addTrans could work again.

    Any help would be appreciate it. I will be working on the program and I will update it here if I find something new.

    import java.awt.Font;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
    import javax.swing.JTextArea;
     
    public class CheckingAccount 
    {
        private double balance;
        private double sCharge;
        private ArrayList<Transaction> transList;
        private static int transCount;  
     
        public CheckingAccount(double initialBalance)
        {
            balance = initialBalance;
            sCharge = 0.0;
            transCount = 0;
        }
     
        public double getBalance()
        {
            return balance;
        }
     
        public void setBalance(double TransAmt, int TransCode)
        {
            if(TransCode == 1)
                balance = balance - TransAmt;
            else //if(tCode == 2)
                balance = balance + TransAmt;
        }
     
        public double getServiceCharge()
        {
            return sCharge;
        }
     
        public void setServiceCharge(double currentServiceCharge)
        {
            sCharge =+ currentServiceCharge;
        }
        public void addTrans(Transaction newTrans)
        {
            [B]transList.add(newTrans);[/B]//On This line 
            //transCount++;
        }
     
        public static int getTransCount()  //returns the current value of transCount;
        {
            return transCount;
        }
     
        public Transaction getTrans(int i) // returns the i-th Transaction object in the list
        {
            return transList.get(i);
        }
     
    }

    This next code is only part of the Panel and only the first if there are 4 if else if statements

    private class RadioButtonListener implements ActionListener
       {
           Transaction transaction;
           CheckingAccount account;
           public void actionPerformed (ActionEvent event)
           {
     
               if(event.getSource() == trans)
               {
                    double checkC = .15, depositC = .10, sCharge = 0.0;
                    String message ="";
                    int TransCode;
                    double TransAmt,pCheck, pDeposit, balance,finalB;
                    int count = 0, transCount = 0;
                    account = new CheckingAccount(Main.balance);
                    DecimalFormat formatter = new DecimalFormat("#0.00");
     
                    //CheckingAccount account = new CheckingAccount (Main.balance);
                    account.setServiceCharge(sCharge);
                    TransCode = Main.getTransCode();
     
                    if(TransCode == 1)
                    {
                    TransAmt = Main.getTransAmt();
                    transaction = new Transaction(transCount,TransCode,TransAmt);
                    [B]account.addTrans(transaction);[/B[B]]//and on this line[/B]
                    pCheck = Main.processCheck(Main.balance, TransAmt);
                    transaction = new Transaction(account.getTransCount(), 3, pCheck);
                    account.addTrans(transaction);
                    balance = pCheck;
                    message = "Transaction:Check in amount of $"+ formatter.format(TransAmt) + "\n"
                                        + "Current Balance : $"+formatter.format(balance) 
                                        +"\n" +"Service Charge: Check---charge $0.15\n";
     
                    if(balance < 500.0){
                        if(count == 0)
                        {
                        sCharge += 5.00;
                        account.setServiceCharge(sCharge);
                        transaction =  new Transaction(transCount,3, 5.00);
                        account.addTrans(transaction);
                        message += "Service Charge : Below $500 --- $5.00\n";
                        }
                        count++;
                    }
                    if (balance <0){
                        sCharge += 10.00;
                        account.setServiceCharge(sCharge);
                        transaction =  new Transaction(transCount,3, 10.00);
                        account.addTrans(transaction);
                        message += "Service Charge : Below $0 --- $10.00\n";
                    }
                    if (balance <= 50.0){
                        message += "Warning: Balance below $50\n";
                    }
                    sCharge += checkC;
                    account.setServiceCharge(sCharge);
                    message += "Tota Service Charge:$"+formatter.format(sCharge);
                    JOptionPane.showMessageDialog(null, message);
                }//first if


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Copy the error and paste it into your post exactly as it appears on your end. Don't edit it, shorten it, paraphrase it, just copy and paste it.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Im getting these errors

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CheckingAccount.addTrans(CheckingAccount.java:45)
    at AccountPanel$RadioButtonListener.actionPerformed(A ccountPanel.java:71)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.JToggleButton$ToggleButtonModel.setPre ssed(JToggleButton.java:308)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

    and it directs me to these lines

    account.addTrans(transaction);

    and

    transList.add(newTrans);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CheckingAccount.addTrans(CheckingAccount.java:45)
    There is a variable with a null value on line 45. Look at line 45 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 45 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Yes, the top two lines are the most relevant:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CheckingAccount.addTrans(CheckingAccount.java:45)

    (Notice your first description, "There is a problem and gives errors like abstract button, defaultbutton, JToggle and others," wasn't that helpful.)

    You have a Null Pointer Exception, or NPE, a common error that new programmers aren't sure how to fix, because they don't understand what the heck it is. When objects are declared, as in:

    MyObject myObject;

    space is allocated for the object in memory and myObject is initialized to the default value of null. Then, when the object is initialized in code, as in:

    myObject = new MyObject();

    the object is defined by the MyObject() constructor and should no longer be null (but it could be).

    Something in the line:

    account.addTrans(transaction);

    is null, either account or transaction. To figure out which it is, inspect your code and/or add a print statements right before the line that print out account and transaction to figure out which hasn't been initialized. Then you'll know which item is null (maybe both are), and then you'll still have to go back through the code and figure out why.

    Good luck!

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Omega_Zero69 (December 20th, 2013)

  7. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Quote Originally Posted by Norm View Post
    There is a variable with a null value on line 45. Look at line 45 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 45 and print out the values of all the variables on that line.
    Well at line 45 its a method that takes a transaction object and inserts it into and arraylist. I see what you are getting at maybe at line 71 which is the the account calling the method and passing the transaction as a parameter. Maybe transaction being passed is null. Thank you for your help I will check if it has anything by calling it and seeing if it can be fixed I will report back with anything new.

    --- Update ---

    Well I tried what you said Norm and GregBrannon I have inserted

    message +=  "Balance is   " + account.getBalance()
                                    +"\nTrans number   " +transaction.getTransNumber()
                                    +"\nTrans id   " +transaction.getTransId()
                                    +"\nTrans Amt  " +transaction.getTransAmount();
                        JOptionPane.showMessageDialog(null,message);

    after transaction = new Transaction (transCount, TransCode, TransAmt)
    and before the account.addTrans(transaction);
    and I am still getting the error

    I believed balance was not going in or transaction after the above statement everything printed out good.
    Could it be it goes null after that

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    I am still getting the error
    Printing the values will NOT keep the error from happening.

    What code is on line 45? Did you add a println() statement before line 45 that printed out the values of ALL the variables used on line 45? What was printed?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Quote Originally Posted by Norm View Post
    Printing the values will NOT keep the error from happening.

    What code is on line 45? Did you add a println() statement before line 45 that printed out the values of ALL the variables used on line 45? What was printed?
    at line 45 there was a TransCount++; then I removed it and place it inside the Listener after each addTrans

    I added the println() statement before transList.add(newTrans) inside the Account method and this is what printed

    Transaction@75b3adec

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

      transList.add(newTrans)
    If that is the line where the NullPointerException happens, then
    There are two variables on that line: transList and newTrans. You need to print the values of BOTH of them to see which is null.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Omega_Zero69 (December 20th, 2013)

  12. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Quote Originally Posted by Norm View Post
      transList.add(newTrans)
    If that is the line where the NullPointerException happens, then
    There are two variables on that line: transList and newTrans. You need to print the values of BOTH of them to see which is null.
    Thank you Norm that really helped transList came out null. Now transList is an arraylist so Im wondering why is it null and why is it not accepting the objects.
    I will come back if I find anything new again thank you Norm.

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    When object reference variables are defined they are given a null value. The code needs to assign a value to the variable to change it.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Yeah I have change a few things and now it accepts the objects from transaction. But now my new problem is the listing which worked fine before the accepting of the transaction objects now it does not display anything.

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    it does not display anything
    How and where does the code try to display things? What statement is used to display the things?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    I have it in the listener right after the adding the transactions as an else if ( event.getsource() = listTrans)

    else if(event.getSource()== listTrans)
               {
                    JTextArea text = new JTextArea();
                    String message = "",type;
                    text.setOpaque(false);
                    text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
                    text.setBorder(null);
                    message +="Trans Count  " + account.getTransCount();
                    JOptionPane.showMessageDialog(null,message);
     
                    DecimalFormat formatter = new DecimalFormat("#0.00");
                    message+="List of Transactions\n\n";
                    message+="ID        Type        Amount";
     
                    for(int num = 0; num < account.getTransCount();num++)
                    {
                        transaction = account.getTrans(num);
                        if( transaction.getTransId() == 1)
                            {
                                type = "Check";
                            }
                            else if (transaction.getTransId() == 2)
                            {
                                type = "Deposit";
                            }
                            else
                            {
                                type = "Svs.Chrg";
                            }
                        message += String.format("%-5s %15s %20s\n", num,type,
                                formatter.format(transaction.getTransAmount()));
                    }
                    text.setText(message);
                    JOptionPane.showMessageDialog(null, text);
               }

    I have also checked what is the count in the array and it says its 0 which is weird since it worked after fixing the adding trans

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Add some println() calls to print out the content of the messages and the data, copy what is printed and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    Omega_Zero69 (December 20th, 2013)

  19. #16
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Does not create a Transaction inside the array. Gui, objects, and arraylist

    Okay then after a few manipulations to the code I have fixed the list Trans and it displays everything perfectly and then had problems with the other 2 listings but now its all good. I will run it a couple of more times to see if there are still any bugs or problems thank you very much everyone who helped out. I greatly appreciate it.

Similar Threads

  1. Replies: 2
    Last Post: September 5th, 2013, 03:43 AM
  2. Trying to create an array of stacks that contain objects
    By jadeclan in forum Collections and Generics
    Replies: 15
    Last Post: October 6th, 2012, 03:30 PM
  3. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  4. Really need some help with Objects inside an ArrayList
    By ISuckSoBad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2011, 12:33 PM
  5. Pick random objects from array/arraylist. Newbie.
    By Sputnik in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 11:59 AM