plss help!!!!! It's 4am and i'm stl up!!!
Hi, not sure what is wrong with my codes....
this is how i want it to work:
--the user enters their details and item details
--when the user clicks on "FindCustomerAndPlaceBid" button, the users details and item details should be displayed.
the error: NullPointerException
ItemPD.java
import java.util.Vector;
public class ItemPD
{
// attribute definitions
private String item_desc;
private double start_bid;
private String condition;
private String customer_id;
private String end_time;
// reference variables point to Customer for this item
private CustomerPD customer;
// Vector simulates a database of Items
static Vector items;
public static void initialize()
{
items = new Vector();
ItemPD aItem;
//ItemPD.initialize(items);
}
// simulate a DBMS - return Vector of all customer instances
public static Vector getAll()
{ return items;}
// custom method to assign a Item to a Customer
public void assignItemPDToCustomerPD(CustomerPD aCustomer)
{
setCustomerPD(aCustomer);// point this Item to the Customer instance
customer.setItemPD(this); // point the Customer to the Item instance
}
// constructor
public ItemPD(String aItem_desc, double aStart_bid, String aCondition, String aCustomer_id, String aEnd_time)
{
// invoke setters to populate attributes
setItem_desc(aItem_desc);
setStart_bid(aStart_bid);
setCondition(aCondition);
setCustomer_id(aCustomer_id);
setEnd_time(aEnd_time);
// initially no Customer for this item - see assignItemToCustomer
setCustomerPD(null);
items.add(this); // simulate add to DBMS
}
// set accessor methods
public void setItem_desc(String newItem_desc)
{
item_desc = newItem_desc;
}
public void setStart_bid(double newStart_bid)
{
start_bid = newStart_bid;
}
public void setCondition(String newCondition)
{
condition = newCondition;
}
public void setCustomer_id(String newCustomer_id)
{
customer_id = newCustomer_id;
}
public void setEnd_time(String newEnd_time)
{
end_time = newEnd_time;
}
public void setCustomerPD(CustomerPD aCustomer)
{customer = aCustomer; }
// get accessor methods
public String getItem_desc()
{
return item_desc;
}
public double getStart_bid()
{
return start_bid;
}
public String getCondition()
{
return condition;
}
public String getCustomer_id()
{
return customer_id;
}
public String getEnd_time()
{
return end_time;
}
public CustomerPD getCustomer()
{ return customer; }
// tellAboutSelf returns attributes in a String instance
public String tellAboutSelf()
{
return (getItem_desc() + ", "
+ Double.toString(getStart_bid()) + ", "
+ getCondition() + ", "
+ getCustomer_id() + ", "
+ getEnd_time());
}
/*// tellAboutSelf returns attributes in a String instance
public String tellAboutSelf()
{
String itemInfo;
itemInfo = "Item Description "
+ getItem_desc() + ", "
+ Double.toString(getStart_bid()) + ", "
+ getCondition() + ", "
+ getCustomer_id() + ", "
+ getEnd_time();
return itemInfo;
}*/
}
CustomerPD
import java.util.Vector;
public class CustomerPD
{
// attribute definitions
private String name;
private String address;
private String phoneNo;
// reference variable for Item instance
private ItemPD item;
// create a Vector instance
static Vector customers;
public static void initialize()
{
customers = new Vector();
CustomerPD aCustomer;
//ItemPD.initialize(customers);
}
// simulate a DBMS - return Vector of all customer instances
public static Vector getAll()
{ return customers;}
// constructor with parameters
public CustomerPD(String aName, String anAddress, String aPhoneNo)
{
// invoke accessors to populate attributes
setName(aName);
setAddress(anAddress);
setPhoneNo(aPhoneNo);
setItemPD(null);
customers.add(this);
}
// get accessors
public String getName()
{ return name;}
public String getAddress()
{ return address;}
public String getPhoneNo()
{ return phoneNo;}
public ItemPD getItemPD()
{ return item;}
// set accessors
public void setName(String newName)
{ name = newName;}
public void setAddress(String newAddress)
{ address = newAddress;}
public void setPhoneNo(String newPhoneNo)
{ phoneNo = newPhoneNo;}
public void setItemPD(ItemPD aItem)
{ item = aItem;}
}
AddCustomer
import java.awt.*; // font, color, Layout managers
import javax.swing.*; // GUI components
import java.awt.event.*; // ActionEvent, ActionListener, WindowAdapter
import java.util.Vector;
public class AddCustomer extends JFrame implements ActionListener
{
// variables needing class scope
JTextField customerNameText, customerAddressText, customerPhoneText;
JButton addItemButton, resetButton, closeButton;
CustomerPD aCustomer;
String customerName, customerAddress, customerPhone;
AuctionUI mainMenu;
//AddItems addItemWindow;
// constructor
public AddCustomer(AuctionUI parentMenu)
{
mainMenu = parentMenu;
Container c = this.getContentPane();
c.setLayout(new GridLayout(3,1));// sets content pane to 3 rows and 1 column
c.setBackground(Color.white);
JPanel centerPanel = new JPanel(new GridLayout(3,2));
centerPanel.setBackground(Color.pink);
JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
lowerPanel.setBackground(Color.white);
// create name
Font defaultFont = c.getFont(); // get font
JLabel nameLabel = new JLabel(" ",SwingConstants.CENTER);
nameLabel.setFont(new Font("Bradley Hand ITC", Font.BOLD,22));
nameLabel.setText("JS Auction House");
c.add(nameLabel); // add name to the Frame
// create TextFields for name, address & phone
customerNameText = new JTextField();
customerAddressText = new JTextField();
customerPhoneText = new JTextField();
// add labels and TextFields to the panels
centerPanel.add(new JLabel("Name: ", SwingConstants.RIGHT));
centerPanel.add(customerNameText);
centerPanel.add(new JLabel("Address: ", SwingConstants.RIGHT));
centerPanel.add(customerAddressText);
centerPanel.add(new JLabel("Phone: ", SwingConstants.RIGHT));
centerPanel.add(customerPhoneText);
c.add(centerPanel); // add center panel to the Frame
// create Buttons for bottom panel
addItemButton= new JButton("Add Item");
resetButton = new JButton("Reset");
closeButton = new JButton("Close");
// add the button to panel
lowerPanel.add(addItemButton);
lowerPanel.add(resetButton);
lowerPanel.add(closeButton);
c.add(lowerPanel); // add lower panel to the Frame
// register frame as listener for button events
addItemButton.addActionListener(this);
resetButton.addActionListener(this);
closeButton.addActionListener(this);
// create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{shutDown();}
}
);
}
// actionPerformed is invoked when a Button is clicked
public void actionPerformed(ActionEvent e)
{
// see which button was clicked
if(e.getSource() == addItemButton)
{
//addCustomer();
//addItem();
addItemAndCustomer();
}
if(e.getSource() == resetButton)
{
clearForm();
}
if(e.getSource() == closeButton)
{
shutDown();
}
}
// invoke addCustomer method
//private void addItem()
private void addItemAndCustomer()
{
customerName = customerNameText.getText();
customerAddress = customerAddressText.getText();
customerPhone = customerPhoneText.getText();
if(customerName.length() == 0 || customerAddress.length() == 0 || customerPhone.length() == 0)
JOptionPane.showMessageDialog(this, "Please Enter All Data ");
else
{
aCustomer = new CustomerPD(customerName, customerAddress, customerPhone);
AddItems addItemWindow = new AddItems(this,aCustomer);
addItemWindow.setSize(550, 350);
addItemWindow.setTitle("Add Item for this Customer");
addItemWindow.setVisible(true);
this.setVisible(false);
}
/*private void addCustomer()
{
AddCustomer addCustomerFrame = new AddCustomer(this);
addCustomerFrame.setSize(500,300);
addCustomerFrame.setTitle("Add A Customer");
addCustomerFrame.setVisible(true);
this.setVisible(false);
}*/
}
public void clearForm()
{
customerNameText.setText("");
customerAddressText.setText("");
customerPhoneText.setText("");
customerNameText.requestFocus();//sets cursor in Name JTextField
}
public void shutDown()
{
mainMenu.setVisible(true);//AuctionUI in focus
this.dispose();//shuts down AddItems window only
}
}
AddItems
import java.awt.*; // sets font, color, Layout managers
import javax.swing.*; // GUI components
import java.awt.event.*; // ActionEvent, ActionListener, WindowAdapter
import java.util.Vector;
public class AddItems extends JFrame implements ActionListener
{
JPanel centerPanel,lowerPanel;
JTextField item_descText, cust_idText;
JTextField start_bidText, conditionText, end_timeText;
JButton addButton, resetButton, closeButton;
// other variables needing class scope
AddCustomer addCustomerWindow;
CustomerPD aCustomer;
String item_desc, cust_id, condition, end_time;
double start_bid;
// constructor
public AddItems(AddCustomer returnToWindow, CustomerPD newCustomer)
{
addCustomerWindow = returnToWindow;
aCustomer = newCustomer;
Container c = this.getContentPane();
c.setLayout(new GridLayout(3,1));
c.setBackground(Color.white);
JPanel centerPanel = new JPanel(new GridLayout(3,2));
centerPanel.setBackground(Color.pink);
JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
lowerPanel.setBackground(Color.white);
// create name
Font defaultFont = c.getFont(); // get font so can restore
JLabel nameLabel = new JLabel(" ",SwingConstants.CENTER);
nameLabel.setFont(new Font("Bradley Hand ITC", Font.BOLD,22));
nameLabel.setText("JS Auction House");
c.add(nameLabel); // add logo to the Frame
// create TextFields for item_
item_descText = new JTextField();
cust_idText = new JTextField();
start_bidText = new JTextField();
conditionText = new JTextField();
end_timeText = new JTextField();
// add labels and TextFields to the panels
centerPanel.add(new JLabel("Item Description: ", SwingConstants.RIGHT));
centerPanel.add(item_descText);
centerPanel.add(new JLabel("Customer ID: ", SwingConstants.RIGHT));
centerPanel.add(cust_idText);
centerPanel.add(new JLabel("Start Bid ?: ", SwingConstants.RIGHT));
centerPanel.add(start_bidText);
centerPanel.add(new JLabel("Condition: ", SwingConstants.RIGHT));
centerPanel.add(conditionText);
centerPanel.add(new JLabel("End Time: ", SwingConstants.RIGHT));
centerPanel.add(end_timeText);
c.add(centerPanel); // add center panel to the Frame
// create Buttons for bottom panel
addButton = new JButton("Add Customer & Item");
resetButton = new JButton("Reset");
closeButton = new JButton("Close");
// add the buttons
lowerPanel.add(addButton);
lowerPanel.add(resetButton);
lowerPanel.add(closeButton);
c.add(lowerPanel); // add lower panel to the Frame
// register frame as listener for events
addButton.addActionListener(this);
resetButton.addActionListener(this);
closeButton.addActionListener(this);
// create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{shutDown();}
}
);
}
// actionPerformed is invoked when a Button is clicked
public void actionPerformed(ActionEvent e)
{
// see which button was clicked
if(e.getSource() == addButton)
{addItemAndCustomer();}
if(e.getSource() == resetButton)
{clearForm(); }
if(e.getSource() == closeButton)
{shutDown();}
}
// invoke addItemAndCustomer method
private void addItemAndCustomer()
{
try
{
start_bid = Double.parseDouble(start_bidText.getText());
item_desc = item_descText.getText();
cust_id = cust_idText.getText();
condition = conditionText.getText();
end_time = end_timeText.getText();
if (item_desc.length() == 0 || cust_id.length() == 0 || condition.length() == 0 || end_time.length() == 0)
JOptionPane.showMessageDialog(this, "Please Enter All Data ");
else
{
JOptionPane.showMessageDialog(this, "Customer and Item added");
addCustomerWindow.clearForm(); // clear customer form
clearForm(); // clear Item form
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Start Bid must be numeric ");
}
}
private void clearForm()
{
item_descText.setText("");
cust_idText.setText("");
start_bidText.setText("");
conditionText.setText("");
end_timeText.setText("");
item_descText.requestFocus();
}
public void shutDown()
{
addCustomerWindow.setVisible(true);
this.dispose();
}
}
FindCustomerAndItem
import java.awt.*; // Font, Color, Layout managers
import javax.swing.*; // GUI components
import java.awt.event.*; // ActionEvent, ActionListener, WindowAdapter
import java.util.Vector;
import javax.swing.event.*;// ListSelectionListener
public class FindCustomerAndItem extends JFrame implements ActionListener, ListSelectionListener
{
// variables needing class scope
Vector customers;
Vector customerNames;
JList customerList;
JLabel customerAddressLabel, customerPhoneLabel, itemInfoLabel;
JTextField bidderText, currentbidText;
JButton placeButton, resetButton, exitButton;
CustomerPD aCustomer;
//ItemPD aItem;
AuctionUI parentMenu;
// constructor
public FindCustomerAndItem(AuctionUI menu)
{
parentMenu = menu;// set reference to main menu
Container c = this.getContentPane();
c.setLayout(new GridLayout(5,1));
c.setBackground(Color.pink);
JPanel centerPanel = new JPanel(new GridLayout(1,2));
centerPanel.setBackground(Color.pink);
JPanel centerRightPanel = new JPanel(new GridLayout(1,2));
centerRightPanel.setBackground(Color.white);
JPanel centerLowerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerLowerPanel.setBackground(Color.pink);
JPanel lowerPanel = new JPanel(new FlowLayout());
lowerPanel.setBackground(Color.white);
// create logo
Font defaultFont = c.getFont(); // get font so can restore
JLabel nameLabel = new JLabel(" ",SwingConstants.CENTER);
nameLabel.setFont(new Font("Bradley Hand ITC", Font.BOLD,22));
nameLabel.setText("JS Auction House");
c.add(nameLabel); // add name to the Frame
// retrieve the Vector of customers
customers = CustomerPD.getAll();
//items = ItemPD.getAll();
// store customer names in a vector
customerNames = new Vector();
for(int i = 0; i < customers.size(); i++)
{
aCustomer = (CustomerPD) customers.get(i); // get customer reference
String customerName = aCustomer.getName(); // get the name
customerNames.add(customerName);// add name to vector
}
// create a list containing customer names
customerList = new JList(customerNames);
// add scroll bars to the list
JScrollPane scrollPaneCustomerList = new JScrollPane(customerList);
centerPanel.add(scrollPaneCustomerList);
// create Labels for address, phone & boat info
customerAddressLabel = new JLabel();
customerPhoneLabel = new JLabel();
itemInfoLabel = new JLabel();
// add labels to panels and panels to the frame
centerRightPanel.add(customerAddressLabel);
centerRightPanel.add(customerPhoneLabel);
centerPanel.add(centerRightPanel);
c.add(centerPanel); // add center panel to the Frame
c.add(itemInfoLabel);
// create center lower panel
bidderText = new JTextField(15);
currentbidText = new JTextField(10);
centerLowerPanel.add(new JLabel("Bidder ID: "));
centerLowerPanel.add(bidderText);
centerLowerPanel.add(new JLabel("Your Bid: "));
centerLowerPanel.add(currentbidText);
c.add(centerLowerPanel);
// create Buttons for bottom panel
placeButton = new JButton("Place Bid");
resetButton = new JButton("Reset");
exitButton = new JButton("Close");
// add the buttons
lowerPanel.add(placeButton);
lowerPanel.add(resetButton);
lowerPanel.add(exitButton);
c.add(lowerPanel); // add lower panel to the Frame
// register frame as listener for button events
placeButton.addActionListener(this);
resetButton.addActionListener(this);
exitButton.addActionListener(this);
// register frame as listener for list selection events
customerList.addListSelectionListener(this);
// create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
shutDown();
}
}
);
}
// actionPerformed is invoked when a Button is clicked
public void actionPerformed(ActionEvent e)
{
// see which button was clicked
if(e.getSource() == placeButton)
{
//placeBid();
}
if(e.getSource() == resetButton)
{
resetForm();
}
if(e.getSource() == exitButton)
{
shutDown();
}
}
// valueChanged invoked when a list item is selected
public void valueChanged(ListSelectionEvent e)
{// see if source was the list
if(e.getSource() == customerList)
findCustomer();
}
// invoke findCustomer method
private void findCustomer()
{
int i = customerList.getSelectedIndex(); // get index of list item selected
aCustomer = (CustomerPD) customers.get(i);
//aItem = (ItemPD)customers.get(i); // Vector instance invoked to reference from customerVector at index i
customerAddressLabel.setText(aCustomer.getAddress());
customerPhoneLabel.setText(aCustomer.getPhoneNo());
ItemPD aItem = aCustomer.getItemPD();
String ItemInfo = aItem.tellAboutSelf();
itemInfoLabel.setText(ItemInfo);
/*ItemPD thisItem = aCustomer.getItemPD();
String itemInfo = thisItem.tellAboutSelf();
itemInfoLabel.setText(itemInfo);*/
///////////itemInfoLabel.setText(aCustomer.getItemPD().tellAboutSelf());
//String itemDetails = thisItem.tellAboutSelf();
//itemInfoLabel.setText(itemDetails);
}
public void resetForm()
{
bidderText.setText("");
currentbidText.setText("");
bidderText.requestFocus();
}
public void shutDown()
{
parentMenu.setVisible(true);
this.dispose();
}
}
AuctionUI
import java.awt.*; // font, color, Layout managers
import javax.swing.*; // GUI components
import java.awt.event.*; // ActionEvent, ActionListener, WindowAdapter
public class AuctionUI extends JFrame implements ActionListener
{
// variables needing class scope
JButton findCustomerButton, addCustomerButton, closeButton;
public static void main(String args[])
{
// create instance of Frame
AuctionUI frame = new AuctionUI();
frame.setSize(550,350);
frame.setTitle("JS House Auction");
frame.setVisible(true);
//CustomerPD.initialize();
}
// constructor
public AuctionUI()
{
CustomerPD.initialize();
Container c = this.getContentPane();
c.setLayout(new GridLayout(2,1));
c.setBackground(Color.white);
JPanel lowerPanel = new JPanel(); // default FlowLayout
lowerPanel.setBackground(Color.pink);
// create name
Font defaultFont = c.getFont(); // get font
JLabel nameLabel = new JLabel(" ",SwingConstants.CENTER);
nameLabel.setFont(new Font("Bradley Hand ITC", Font.BOLD,22));
nameLabel.setText("JS Auction House");
c.add(nameLabel); // add name to the Frame
// create Buttons for bottom panel
findCustomerButton = new JButton("Find Item and Place bid");
addCustomerButton = new JButton("Add a Customer & Item");
closeButton = new JButton("Close");
// add the buttons
lowerPanel.add(findCustomerButton);
lowerPanel.add(addCustomerButton);
lowerPanel.add(closeButton);
c.add(lowerPanel); // add lower panel to the Frame
// register frame as listener for button events
findCustomerButton.addActionListener(this);
addCustomerButton.addActionListener(this);
closeButton.addActionListener(this);
// create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
shutDown();
}
});
}
// actionPerformed is invoked when a Button is clicked
public void actionPerformed(ActionEvent e)
{
// see which button was clicked
if(e.getSource() == findCustomerButton)
{
findCustomer();
}
if(e.getSource() == addCustomerButton)
{
addCustomer();
}
if(e.getSource() == closeButton)
{
shutDown();
}
}
// invoke findCustomer method when button is clicked
private void findCustomer()
{
FindCustomerAndItem findCustomerFrame = new FindCustomerAndItem(this);
findCustomerFrame.setSize(550,350);
findCustomerFrame.setTitle("Find A Customer and Item");
findCustomerFrame.setVisible(true);
this.setVisible(false);
}
// invoke addCustomer method to add customers to database
private void addCustomer()
{
AddCustomer addCustomerFrame = new AddCustomer(this);
addCustomerFrame.setSize(500,300);
addCustomerFrame.setTitle("Add A Customer");
addCustomerFrame.setVisible(true);
this.setVisible(false);
}
public void shutDown()
{
this.dispose();
System.exit(0); // terminate
}
}

