Computer Science

posted by .

I am making a object oriented program and hashing the list of make of an electronic and type to the JCombobox...i am getting this number Exception error...it will nice if you could tell why am i getting it eventhough i am doing the casting and everything. Try to help ASAP! thank you. Here's my code.
The following code is for buyPrinterDialog box.
public class BuyPrinters extends JDialog {

/**
*
*/
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private JTextField textField;
String[] make = {"Lexmark", "HP" , "Apple" , "Brother" , "Canon", "Xerox"};
String [] type = {"Inkjet", "Laserjet"};
HashMap LexmarkPriceList = null;
HashMap HPPriceList = null;
HashMap ApplePriceList = null;
HashMap BrotherPriceList = null;
HashMap CanonPriceList = null;
HashMap XeroxPriceList = null;



/**
* Launch the application.
*/
MyFrame parent = null;
public static void main(String[] args) {
try {
BuyPrinters dialog = new BuyPrinters(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the dialog.
*/
JComboBox makeComboBox = new JComboBox();
JComboBox typeComboBox = new JComboBox();
public BuyPrinters(MyFrame parent) {

this.parent = parent;

setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Gaurav sharma\\workspace\\Gaurav.Lovepreet.Harpreet.Java2.Project\\pics and icons\\printer_64.png"));
setTitle("BuyPrinters");
setBounds(100, 100, 525, 263);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(new Color(204, 204, 204));
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblMake = new JLabel("Make:");
lblMake.setFont(new Font("Tahoma", Font.BOLD, 12));
lblMake.setBounds(10, 11, 236, 24);
contentPanel.add(lblMake);
}
{

makeComboBox.setBounds(10, 46, 305, 20);
makeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updatePriceBox();
}


});

contentPanel.add(makeComboBox);
}


{
JLabel lblType = new JLabel("Type:");
lblType.setFont(new Font("Tahoma", Font.BOLD, 12));
lblType.setBounds(10, 77, 236, 24);
contentPanel.add(lblType);
}
{

typeComboBox.setBounds(10, 112, 305, 20);

typeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updatePriceBox();
}


});
contentPanel.add(typeComboBox);
}
{
textField = new JTextField();
textField.setEditable(false);
textField.setBounds(66, 155, 86, 20);
contentPanel.add(textField);
textField.setColumns(10);
}
{
JLabel lblPrice = new JLabel("Price:");
lblPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
lblPrice.setBounds(10, 157, 46, 14);
contentPanel.add(lblPrice);
}
{
JPanel panel = new JPanel();
panel.setBackground(new Color(102, 255, 255));
panel.setBounds(348, 21, 151, 151);
contentPanel.add(panel);
panel.setLayout(null);
{
JButton btnNewButton = new JButton("");
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Gaurav sharma\\workspace\\Gaurav.Lovepreet.Harpreet.Java2.Project\\pics and icons\\printer_64.png"));
btnNewButton.setBounds(0, 0, 151, 151);
panel.add(btnNewButton);
}
}
{
JPanel panel = new JPanel();
panel.setBackground(new Color(255, 153, 255));
panel.setBounds(0, 11, 338, 170);
contentPanel.add(panel);
}

JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleOk();
}
});
btnOk.setBounds(318, 191, 89, 23);
contentPanel.add(btnOk);

JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleCancel();
}
});
btnCancel.setBounds(417, 191, 89, 23);
contentPanel.add(btnCancel);
{
JPanel buttonPane = new JPanel();
buttonPane.setBackground(new Color(102, 255, 255));
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
//getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleCancel();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}

createMakePriceLists();

initializeMakeBox();
updatePriceBox();




}

private void handleCancel()
{
this.setVisible(false);
}


//Handling the Okay button.
private void handleOk()
{
this.setVisible(false);

Float price = Float.parseFloat(textField.getText());


Date manufactureDate = new Date();
int warrantyPeriodInYears = 3;


String make = (String) makeComboBox.getSelectedItem();
InkJet printer1 = new InkJet( price, manufactureDate, warrantyPeriodInYears, "type" ,make);

if (parent != null)
{
parent.updatePurchasedItemList(printer1);
}



}



//Creating Make pricelist which depends on the type they buy.
private void createMakePriceLists()
{
LexmarkPriceList = new HashMap();
LexmarkPriceList.put("Inkjet", 659f);
LexmarkPriceList.put("Laserjet", 799f);
HPPriceList = new HashMap();
HPPriceList.put("Inkjet", 299f);
HPPriceList.put("Laserjet", 359f);
ApplePriceList = new HashMap();
ApplePriceList.put("Inkjet", 999f);
ApplePriceList.put("Laserjet", 1089f);
BrotherPriceList = new HashMap();
BrotherPriceList.put("Inkjet", 99f);
BrotherPriceList.put("Laserjet", 159f);
CanonPriceList = new HashMap();
CanonPriceList.put("Inkjet", 299f);
CanonPriceList.put("Laserjet", 289f);
XeroxPriceList = new HashMap();
XeroxPriceList.put("Inkjet", 1299f);
XeroxPriceList.put("Laserjet", 1599f);


}



//Creating the type Price list which changes the price depending on whether the user buys laser or inkjet.


private void updatePriceBox()
{
String make = (String) makeComboBox.getSelectedItem();
System.out.println("update price box: selected make:"+make);
HashMap map = new HashMap() ;
if (make.equals("Lexmark"))
map = LexmarkPriceList;
if (make.equals("HP"))
map = HPPriceList;
if (make.equals("Xerox"))
map = XeroxPriceList;
if (make.equals("Brother"))
map = BrotherPriceList;
if (make.equals("Canon"))
map = CanonPriceList;
if (make.equals("Apple"))
map.equals(ApplePriceList);


makeComboBox.getSelectedItem();
Float price = (Float) map.get(make);
System.out.println(price);
textField.setText(""+ price);
}

//Initialize the Make box.
private void initializeMakeBox()
{
makeComboBox.removeAllItems();
for (String s : make) {
makeComboBox.addItem(s);
}

typeComboBox.removeAllItems();
for (String s : type) {
typeComboBox.addItem(s);

}


}
}

  • Computer Science -

    Number exception errors are typically execution time errors. You will need to check your data for any irregular data input.

    The way to go about debugging it is to find out from the message on which source code line the error has occurred, and then trace back from there the probable variable that caused the error, and finally the data, or data conversion.

    Even though you may have cast every numeric value, if the code is trying to parse a String or null into a number, the run-time error will occur.

    Give it a trace and see if you can find the source of the error.

  • Computer Science -

    Also, typically, the kind of statements as:

    Float price = Float.parseFloat(textField.getText());


    would be best put inside a try-loop to catch any unexpected run-time errors.

  • Computer Science -

    thanks...yes i found the error. It was basically trying to parse a null which is a string. Yes i can try and catch the exception but haven't learned it but thanks for idea...i think i mite do that.

  • Computer Science -

    Good to hear!
    If you have other questions, feel free!

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. computer

    Object-Oriented Data and Processes • Identify a task you perform regularly, such as cooking, mowing the lawn, or driving a car. • Write a short, structured design (pseudocode only) that accomplishes this task. • Think about this …
  2. Computer Programming

    Project: I need help getting start on this project. What I have so far is a mess. It's Do While loops and nested loops. Table 6.1 shows the different grads of eggs and the minimum weight required for each classification. Write a program …
  3. computer programming

    Write a program that simulates the dial of a phone number. The program will input a phone number and the program will acknowledge the call by either writing an error message or the 8 digit phone number to the console window. The phone …
  4. computer science

    to write a report on Object-Oriented Systems. The report should focus on the following systems including definition, applications, and advantages and disadvantages. • Object-oriented Systems • Object-oriented database • Object-oriented …
  5. Probability

    A card is drawn from a standard deck of cards. Find P(A U B) in each part. A= {getting a heart}, B= {getting an even number} A= {getting a club}, B= {getting a red card} A= {getting an ace}, B= getting a black card} A= {getting a prime}, …
  6. Programming

    Hello All i am new in dynamics Ax when i am going to connect the C# with Dynamics Ax using logon method i am getting the following error when connecting with DynAX from the it's own server so it connects but when i want to connect …
  7. Computer Programming

    Your city's Parking Violation Bureau wants you to write a program to compute fines for parking violations. There are four types of violation: type A carries a fine of $10, type B carries fine of $20, type C carries a fine of $30, and …
  8. Public Speaking

    If your listening preference is to “cut to the chase” by getting to the point quickly, then you are a(n) __________ listener. A. time-oriented*** B. action-oriented C. content-oriented D. people-oriented
  9. computer science

    Acarrentalcompanyusesthefollowingtabletohelpcomputerentalfees. TABLE 9.3 Car Type Chevrolet Corvette Pontiac Buick Oldsmobile Cadillac Rate per Day 50.00 85.00 53.00 60.00 60.00 70.00 Rate per Mile 0.27 0.45 0.32 0.32 0.35 0.40 Write …
  10. computer science

    Acarrentalcompanyusesthefollowingtabletohelpcomputerentalfees. TABLE 9.3 Car Type Chevrolet Corvette Pontiac Buick Oldsmobile Cadillac Rate per Day 50.00 85.00 53.00 60.00 60.00 70.00 Rate per Mile 0.27 0.45 0.32 0.32 0.35 0.40 Write …

More Similar Questions