I tried running the following code. It compiles, but throws a ClassCastException. I would be really glad if anyone can help me figure out why.
double costprice = 0;
Object[] possibilities = null;
costprice = (double) JOptionPane.showInputDialog(
alphaPOS,
"Cost Price:",
"Enter Values",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"");
JOptionPane.showInputDialog()
returns a Object
(credits to @SeleenVirtuose) which cannot be cast to a double
, use Double.parseDouble()
to parse a String as a double.
costprice = Double.parseDouble(JOptionPane.showInputDialog(
alphaPOS,
"Cost Price:",
"Enter Values",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
""));
Also, you can all of this on one line
As you are declaring the double
variable and then setting straight away, you might as well declare it and assign the new value in one line
double costprice = Double.parseDouble(JOptionPane.showInputDialog(
alphaPOS,
"Cost Price:",
"Enter Values",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
""));
Also, as the possibilities
variable is null, just pass null
as the parameter (unless you have changed the variable somewhere else)
double costprice = Double.parseDouble(JOptionPane.showInputDialog(
alphaPOS,
"Cost Price:",
"Enter Values",
JOptionPane.PLAIN_MESSAGE,
null,
nulll,
""));