Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 05:39 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:35 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Averages of user inputed values (Need Help) Zebra New To Java 2 04-16-2008 02:51 PM
JLabel append? Jononomous New To Java 0 04-07-2008 08:41 PM
Simple append question Rageagainst20 New To Java 0 12-21-2007 12:40 AM
How can I save the content of textfield in a text file? fred Java Applets 4 08-09-2007 02:30 PM
Help with removing somthing once inputed. fegiflu New To Java 8 07-26-2007 11:58 PM


All times are GMT +3. The time now is 01:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org