PDA

View Full Version : help: Java nullpointer problem


kyuu
06-03-2006, 06:04 AM
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've found out is that the problem lies in my ActionListener...or at least i think so.:confused: )
here's the source code for the main part of the program:
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;

}

Gox
06-03-2006, 09:31 PM
I 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.

EDIT: Doh, 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.

kyuu
06-04-2006, 01:06 AM
thanks

i forgot to mention this but, if anyone needs the other parts of my program to test it or anything just ask me :)

in general, the program compiles fine (no errors picked up there), but doesn't run properly (<--purchase button doesn't work >_>)
I figured from testing it with System.out.println that browsearea.append("insert whatever text here"); doesn't seem to be working. and (I guess) is somehow causing the nullpointerexception? :confused:

daniel_g
06-05-2006, 02:09 AM
I don't know much about nullpointer exception, but I would try this:

public void actionPerformed(ActionEvent event)
{
System.out.println("TEST1");
String wstate = instate.getText();
System.out.println("TEST2");
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
System.out.println("TEST3);
browsearea.append("You have purchased a " + wstate + " " + wbrand + " " + wtype + " for $" + wprice); //doesn't seem to work
}


Did all 3 tests go through ok?

kyuu
06-05-2006, 06:31 PM
ah...nevermind i got the problem fixed (i should've noticed it before^^; )
thanks anyway.

i have a second question if you guys don't mind,
from the same class (DealerFrame) i want to add items to a second JComboBox when an item from the first JComboBox is selected, like so:

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:");
selectcars = new JComboBox();


class startCheck implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if((String)browsecombo.getSelectedItem() == "Car Type")
{
System.out.println("test");
selectcars.addItem("Family Cars");
selectcars.addItem("Minivans");
selectcars.addItem("Vans");
selectcars.addItem("SUV");
}
if((String)browsecombo.getSelectedItem() == "Manufacturer")
{
System.out.println("test");
selectcars.addItem("BMW");
selectcars.addItem("Toyota");
selectcars.addItem("GM");
selectcars.addItem("Mercedes");
}
if((String)browsecombo.getSelectedItem() == "New Cars")
{
browsearea.append("\ntest");
}
if((String)browsecombo.getSelectedItem() == "Used Cars")
{
browsearea.append("\ntest");
}
}
}
startCheck listener = new startCheck();
browsecombo.addActionListener(listener);
with my newly fixed code though, this doesn't seem to be working, any suggestions here?

rpgfan3233
06-05-2006, 06:55 PM
Have you tried using browsecombo.getSelectedItem().toString() instead of (String)browsecombo.getSelectedItem() ?
IMHO, what you are trying to do with the code you just posted is similar to:
(int)stringVariable
when you should be using
Integer.parseInt(stringVariable)

Just a suggestion. . .

kyuu
06-06-2006, 04:08 AM
true true, I just got that (the (String)combobox.getSelectedItem()) out of the textbook I was using, thanks for the suggestion though :)

does anyone else have any suggestions on how to get my second combo box to work properly? (I still can't fix it to work)