Results 1 to 7 of 7
  1. #1
    veryCHEWEY is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Button doesn't behave

    Hey, I think i'm on the last leg in my code which basically takes 3 user given inputs and outputs in an adjacent( and blank) label. It basically multiplies the quantity and the cost and outputs it in a label for total. I guess it's supposed to work like a receipt...? Sorry if it's messy, I hope the white space and small amount of comments help. If it makes a difference, I'm using BlueJ
    Edit: I took an example that calculates the average of rainfall and essentially changed the code from that into this since they both took a user input, did some math, and out put it in a label. There might be some extra code I'm not aware of or variables that don't match.
    The issue I have would be when I use the button, I get many exceptions.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class Inventory
    {
        // The Math and the button
        private static class ActionHandler implements ActionListener
       {
           public void actionPerformed(ActionEvent event)
           {
               double amount;
               double cost;
               double invoiceNum;
               double totSum;
               
               amount = Double.valueOf(Quantity.getText()).doubleValue();
               cost = Double.valueOf(UnitPrice.getText()).doubleValue();
               invoiceNum = Double.valueOf(Invoice.getText()).doubleValue();
               totSum = amount * cost;
               
               //outputs
               finInvoice.setText(" " +  invoiceNum);
               finQuant.setText(" " +  amount);
               finUnPr.setText(" " +  cost);
               finTot.setText(" " +  totSum);
               //inputs clear
               Invoice.setText(" ");
               Quantity.setText(" ");
               UnitPrice.setText(" ");
               
           }
       }
       //Declares variables for the frame
       private static Frame inputFrame;
       private static Label finInvoice;
       private static Label finQuant;
       private static Label finUnPr;
       private static Label finTot;
       private static TextField Invoice;
       private static TextField Quantity;
       private static TextField UnitPrice;
       private static TextField inputField;
       
       //The Frame
       public static void main (String[] args)
       {
           Label invoice;
           Label finInvoice;
           Label quantity;
           Label finQuant;
           Label unitPrice;
           Label finUnPr;
           Label finTot;
           Label total;
           Button enter;
           ActionHandler action;
           
    
    
           inputFrame = new Frame();
           invoice = new Label ("Invoice Number: ");
           Invoice = new TextField ("");
           finInvoice = new Label ("", Label.RIGHT);
           
           quantity = new Label ("Quantity: ");
           Quantity = new TextField ("");
           finQuant = new Label ("", Label.RIGHT);
           
           unitPrice = new Label ("Unitprice: ");
           UnitPrice = new TextField ("");
           finUnPr = new Label ("", Label.RIGHT);
           
           total = new Label ("Total: ");
           finTot = new Label ("", Label.RIGHT);
           
           //Makes the variables and inputs.output appear
           enter = new Button("Enter");
           enter.setActionCommand("enter");
           action = new ActionHandler();
           enter.addActionListener(action);
           
           inputFrame.setLayout(new GridLayout(5,3));
           inputFrame.add(invoice);
           inputFrame.add(Invoice);
           inputFrame.add(finInvoice);
           
           inputFrame.add(quantity);
           inputFrame.add(Quantity);
           inputFrame.add(finQuant);
           
           inputFrame.add(unitPrice);
           inputFrame.add(UnitPrice);
           inputFrame.add(finUnPr);
           
           inputFrame.add(total);
           inputFrame.add(finTot);
           
           inputFrame.add(enter);
           inputFrame.pack();
           inputFrame.show();
           inputFrame.addWindowListener(new WindowAdapter()
           {
               public void windowClosing(WindowEvent event)
               {
                   inputFrame.dispose();
                   System.exit(0);
               }
           });
       }
    }
    Last edited by veryCHEWEY; 02-22-2010 at 02:44 PM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    Hi,
    Please go thru the basics before working.

    where have u declared all this
    finInvoice.setText(" " + invoiceNum);
    finQuant.setText(" " + amount);
    finUnPr.setText(" " + cost);
    finTot.setText(" " + totSum);
    //inputs clear
    Invoice.setText(" ");
    Quantity.setText(" ");
    UnitPrice.setText(" ");


    In the method scope u have instantiated and trying to use in actionperformed.

    How it will work? All null references right?
    Ramya:cool:

  3. #3
    veryCHEWEY is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
           Label invoice;
           Label finInvoice;
           Label quantity;
           Label finQuant;
           Label unitPrice;
           Label finUnPr;
           Label finTot;
           Label total;
    It's after the first block of code, If that's the problem, I'll get to it.

    Edit: That would show up when I compile it, I don't get an error when I compile this code, only after I run it, put in input, hit the button. This is confusing, I know, but it shows up that way when I took this from an example my professor gave me.

    Edit #2: Yes, I get NullPointerException.
    basically this 4x

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Inventory$ActionHandler.actionPerformed(Inventory. java:29)
    at java.awt.Button.processActionEvent(Button.java:392 )
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Last edited by veryCHEWEY; 02-22-2010 at 02:57 PM.

  4. #4
    veryCHEWEY is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry for double post, wanted to get your attention and point it to edit #2.

    In the method scope u have instantiated and trying to use in actionperformed.
    I don't understand.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    you have got confused with the flow.why u need all static and variable declation in confused way
    Follow the code flow like this.
    1.Iventory class implements the ActionListenener.
    2.Declare all the GUI components and Frame and in constructor instantiate it.
    3.Add all the component to Frame.
    3.Register the buttton to actionlistener.
    4.Then add the actionperformed method and add the code whatever u want related to button.


    Follow the above fashion.A single class can do whatever u want.Be simple when u start up then enhance .Put comments properly.
    Ramya:cool:

  6. #6
    veryCHEWEY is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    So my problem would be a matter of order and general neatness? If it is... oops, thx. I only followed this form because I used an example as a template and extended and changed that in order to make this problem code.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by veryCHEWEY View Post
    Button doesn't behave
    So spank him. Enough of these "alternative" child-rearing methods already.

Similar Threads

  1. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 01:28 AM
  2. default button SWT
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-07-2009, 04:12 PM
  3. Button Actions
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2009, 09:50 PM
  4. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:05 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
  •