Results 1 to 2 of 2
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Append text inputed in the textfield into a TextArea

    I'm writing this Car Dealer program that lets a user purchase and browse through cars. while testing my program i found that when you attempt to purchase a car (which is supposed append text inputed in the textfield into a TextArea), you get a NullPointerException error
    I spent maybe about 2 and a half hours trying to figure out what's wrong and still can't figure it out, can anyone give me a hand here?^^;; (the most i have found out is that the problem lies in my ActionListener...or at least i think so.)

    Here's the source code:

    Java Code:
    import java.awt.*;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DealerFrame extends JFrame
    {
        public DealerFrame()
        {
            /*NewCars ncar1 = new NewCars("BMW", "Family Car", "New", "Z3", 35000);
            NewCars ncar2 = new NewCars("Mercedes", "SUV", "New", "LE", 35000);
            OldCars ocar1 = new OldCars("Volvo", "Family Car", "Old", "Z3", 35000);
            OldCars ocar2 = new OldCars("Toyota", "Family Car", "Old", "Camry", 20000);
            Object[] ncarArray = {ncar1, ncar2, ocar1, ocar2}; */
            Dealer carDealer = new Dealer();
            carDealer.addCar(new NewCars("BMW", "Family Car", "New", "Z3", 35000));
            carDealer.addCar(new OldCars("Toyota", "Family Car", "New", "Camry", 20000));
            
            JTextArea browsearea = new JTextArea("Welcome to __ Car Dealer we currently have: " + carDealer.getTotal() + " cars in stock.\n", 20, 10);
            getContentPane().add(browsearea, BorderLayout.CENTER);
            getContentPane().add(createDealerPanel(), BorderLayout.NORTH);
            getContentPane().add(createPurchasePanel(), BorderLayout.SOUTH);
            pack();
            show();
        }
        
        public JPanel createDealerPanel()
        {
            JLabel browselabel = new JLabel("Select a method to browse by:");
            browsecombo = new JComboBox();
            browsecombo.addItem("Car Type");
            browsecombo.addItem("Manufacturer");
            browsecombo.addItem("New Cars");
            browsecombo.addItem("Used Cars");
            
            JLabel carsets = new JLabel("Select the Type/Manufacturer you want to browse:\n");
            selectcars = new JComboBox();
    
            class startCheck implements ActionListener
            {  
                public void actionPerformed(ActionEvent event)
                {
                    selectcars.removeAllItems();
                    if((String)browsecombo.getSelectedItem() == "Car Type")
                    {
                        /*selectcars.addItem("Family Cars");
                        selectcars.addItem("Minivans");
                        selectcars.addItem("Vans");
                        selectcars.addItem("SUV"); */
                        System.out.println("test");
                        //browsearea.append("test");
                    }
                    if((String)browsecombo.getSelectedItem() == "Manufacturer")
                    {
                        System.out.println("test");
                        //browsearea.append("test");                   
                        /*selectcars.addItem("BMW");
                        selectcars.addItem("Toyota");
                        selectcars.addItem("GM");
                        selectcars.addItem("Mercedes");*/
                    }
                    if((String)browsecombo.getSelectedItem() == "New Cars")
                    {
                        System.out.println("test");
                        //browsearea.append("test");                   
                        //browsearea.append((String)Object[0] + "\n" + (String)Object[1]);
                    }
                    if((String)browsecombo.getSelectedItem() == "Old Cars")
                    {
                        System.out.println("test");
                        //browsearea.append("test");                    
                    }
                }
            }
            startCheck listener = new startCheck();
            browsecombo.addActionListener(listener);
            
            /*class searchCar implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    
                    
                }
            }
            searchCar listener2 = new searchCar();
            selectcars.addActionListener(listener2);*/
            
            JPanel browsePanel1 = new JPanel();
            browsePanel1.add(browselabel);
            browsePanel1.add(browsecombo);
            browsePanel1.add(carsets);
            browsePanel1.add(selectcars);
            return browsePanel1;
        }
        
        public JPanel createPurchasePanel()
        {
            JLabel purchaseLabel = new JLabel("Purchase a Car:");
            JLabel inmanufacLabel = new JLabel("Brand:");
            final JTextField inmanufac = new JTextField(5);
            
            JLabel intypeLabel = new JLabel("Type:");
            final JTextField intype = new JTextField(5);
            
            JLabel instateLabel = new JLabel("New/Old?:");
            final JTextField instate = new JTextField(5);
            
            JLabel inpriceLabel = new JLabel("Price:");
            final JTextField inprice = new JTextField(5);
            
            JButton purchaseButton = new JButton("Purchase Car");      
            class purchaseListener implements ActionListener //problem lies somewhere here?
            {   
                public void actionPerformed(ActionEvent event)
                {
                    String wstate = instate.getText();
                    String wbrand = inmanufac.getText();
                    String wtype = intype.getText();
                    String wprice = inprice.getText();
                    System.out.println("test" +  wstate + " " + wbrand + " " + wtype + " " + wprice ); //test line to check if it prints
                    browsearea.append("You have purchased a " + wstate + " " + wbrand + " " + wtype + " for $" + wprice); //doesn't seem to work
                }
            }
            purchaseListener plistener = new purchaseListener();
            purchaseButton.addActionListener(plistener);
            
            JPanel browsePanel2 = new JPanel();
            browsePanel2.add(purchaseLabel);
            browsePanel2.add(inmanufacLabel);
            browsePanel2.add(inmanufac);
            browsePanel2.add(intypeLabel);
            browsePanel2.add(intype);
            browsePanel2.add(instateLabel);
            browsePanel2.add(instate);
            browsePanel2.add(inpriceLabel);
            browsePanel2.add(inprice);
            browsePanel2.add(purchaseButton);
            
            return browsePanel2;
        }
       
        private JTextArea browsearea;
        private JTextField inmanufac;
        private JTextField intype;
        private JTextField inprice;
        private JComboBox browsecombo;
        private JComboBox selectcars;
    
    }
    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Don't have the means in which to test or modify your code with the computer I'm currently at, but I'll throw out a couple of ideas based on what I saw.

    Your instance variables "browsearea" etc are declared at the bottom of your code. I'm not sure whether this makes a difference for sure, but it might be the case that you're trying to use a variable that hasn't been declared yet. However, after saying that your error doesn't seem to imply this. If this were a problem I'd expect a compilation error rather than a runtime error. So here's another thought.

    You have declared "browsearea" but I don't see that you've actually instantiated it. Thus, causing a problem.

    I now see that you've indeed instantiated your variable. So I guess you can safely disregard any of my thoughts. Sorry to have wasted your time. I'll take a look at it later if the question still isn't resolved.
    Greetings.

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  2. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:51 PM
  3. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 07:41 PM
  4. Simple append question
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-20-2007, 11:40 PM
  5. Help with removing somthing once inputed.
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-26-2007, 10:58 PM

Posting Permissions

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