'Beginner Error with JOptionPane showmessagedialog
I have been following a tutorial on YouTube for Java using Eclipse.
Currently I am trying to multiply two doubles and output the result as a double using JOptionPane.showMessageDialog but it is giving me an error.
Here is my code:
import javax.swing.JOptionPane;
public class Variables {
public static void main(String arg[])
{
double length = 3;
double width = 2;
double area = length*width;
JOptionPane myIO = new JOptionPane();
myIO.showMessageDialog(null, area);
}
}
and the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method showMessageDialog(Component, Object) in the type JOptionPane
is not applicable for the arguments (null, double)
at Variables.main(Variables.java:11)
I have exactly what I see in the tutorial so I don't have a clue what I am doing wrong. I feel like I didn't import some library or something that I should have but it seems like Eclipse recognizes JOptionPane and showMessageDialog.
Solution 1:[1]
Change your main to this:
public static void main(String arg[])
{
double length = 3;
double width = 2;
double area = length*width;
JOptionPane.showMessageDialog(null, area);
}
When using JOptionPane we do not need any declaration.
Solution 2:[2]
There are many problems with this code, and I would suggest reading the official Java tutorials rather than the one you looked at.
First of all, you shouldn't even try to run code that doesn't compile. Open the Problems view in Eclipse, and don't even think about running your code if there is one compilation error listed in this view.
Then read the compiler error message and try to understand them to fix them. The problem here is that you're probably using a very, very old version of the JDK (pre Java 5), which doesn't automatically boxes primitives (double) into their wrapper type (Double). Download the latest version of the JDK, and configure eclipse to use this new version.
But there are other problems:
- you shouldn't create a new JOptionPane to invoke the static method showMessageDialog(). Just use JOptionPane.showMessageDialog().
- you shouldn't use Swing outside of the event dispatch thread. Given that you're a newbie, don't use Swing at all, and only yse System.out.println() calls to print values on the screen for now.
Solution 3:[3]
Try changing double area to Double area.
double is a primitive and thus does not inherit off of Object, which is the parameter type showMessageDialog expects.
Double is a wrapper class for double and, as all objects, inherits off of Object.
Java often has no problem converting from Double to double and vice versa, but the fact that it takes an argument of type Object may imply that you didn't actually want to do what you did.
I'll note that this works in NetBeans (Java 7) and IDEOne (Java 6) (except for an expected runtime error), so maybe Eclipse is in the wrong here, or you're using an old version of Java. When it comes to Java, you should always be running the latest, unless you have a specific reason not to. Forward-compatibility is only a problem by exception.
Solution 4:[4]
Cast an int type to the area double like so:
import javax.swing.JOptionPane;
public class Variables {
public static void main(String arg[])
{
double length = 3;
double width = 2;
double area = length*width;
JOptionPane myIO = new JOptionPane();
JOptionPane.showMessageDialog(null, (int)area);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tdorno |
| Solution 2 | JB Nizet |
| Solution 3 | |
| Solution 4 |
