Posted by Gaurav on Tuesday, April 10, 2012 at 9:59pm.
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 - MathMate, Tuesday, April 10, 2012 at 11:04pm
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 - MathMate, Tuesday, April 10, 2012 at 11:12pm
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 - Gaurav, Wednesday, April 11, 2012 at 7:24pm
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 - MathMate, Wednesday, April 11, 2012 at 9:21pm
Good to hear!
If you have other questions, feel free!
Answer this Question
Related Questions
computer science - to write a report on Object-Oriented Systems. The report ...
computer - Object-Oriented Data and Processes Identify a task you perform...
Medical Information Management - Medical records at willow nursing home have the...
computer science - write a program to select the classes that you are taking(Ex...
communication - paragraph response describing the advantages and disadvantages ...
object oriented programming 1 - write a c++ program that calculates the area of ...
Computer Program - What do I have to do if I want to make the sound volumes of ...
object oriented programming - We want to define a class clockType, to implement...
computers in day care - A software program features an electronic coloring book...
Computer Club Ideas (please read) - Is there any sites for middle or high ...
For Further Reading