Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an inconvertible type error?

If I use this class:

public class BooleanTest {
    public static void main(String args[]) {
        final Object[] objarray = new Object[2];
        try {
            objarray[0] = "Hello World!";
            objarray[1] = false;
        } catch (NullPointerException e) {
        }
        boolean bool = (boolean) objarray[1];
    }
}

It works fine and I can assign that boolean no problem. Why can I not do the same thing when asking my user for a password?

final Object result[] = new Object[2];
try {
    java.awt.EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(3,0));
            JLabel label = new JLabel();

            label.setHorizontalAlignment(SwingConstants.LEADING);
            JTextField input = new JTextField();

            input.setHorizontalAlignment(SwingConstants.CENTER);
            JCheckBox checkbox = new JCheckBox("Pair with this device");
            checkbox.setHorizontalAlignment(SwingConstants.LEADING);
            panel.add(label);
            panel.add(input);
            panel.add(checkbox);
            if (wrong) {
                label.setText("Wrong password. Please enter the password from the other device:");
            } else {
                label.setText("Please enter the password from the other device:");
            }
            int response = JOptionPane.showConfirmDialog(SendGUI.this, panel, "Enter password", JOptionPane.OK_CANCEL_OPTION);
            if (response == JOptionPane.OK_OPTION) {
                result[0] = input.getText();
                result[1] = (boolean)checkbox.isSelected();
            } else {
                result[0] = null;
                result[1] = false;
            }
        }
    });
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
}
boolean pair = (boolean)result[1]; //inconvertible type, expected boolean found Object

As far as I can see I'm doing the same thing in both cases but the first example compiles fine while the second example does not.

like image 268
ldam Avatar asked Apr 02 '13 16:04

ldam


People also ask

How to solve incompatible type error in Java?

Swapping the inappropriate constructor call with an instance of the correct type solves the issue, as shown in Fig. 6(b). Fig. 6 also serves as an example to show how the incompatible types error is, in fact, a generalization of the method X in class Y cannot be applied to given types error explored in [4].

What does error .class expected mean?

The class interface or enum expected error is a compile-time error in Java which arises due to curly braces. Typically, this error occurs when there is an additional curly brace at the end of the program.


1 Answers

You're using different compiler options. You must be. Both pieces of code compile under Java 7 rules; neither compiles under Java 6 rules. For example, taking your first piece of code (the one that you say compiles for you):

c:\Users\Jon\Test>javac -source 1.7 BooleanTest.java

(No console output, i.e. no errors)

c:\Users\Jon\Test>javac -source 1.6 BooleanTest.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
BooleanTest.java:10: error: inconvertible types
        boolean bool = (boolean) objarray[1];
                                         ^
  required: boolean
  found:    Object
1 error
1 warning

EDIT: I believe the change is in section 5.5 of the JLS (Casting conversions).

The Java 7 version includes:

Casting contexts allow the use of one of:

  • ...
  • a narrowing reference conversion (§5.1.6) optionally followed by either an unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)

The JLS 3rd edition (Java 5 and 6, basically) includes:

Casting contexts allow the use of one of:

  • ...
  • a narrowing reference conversion (§5.1.6) optionally followed by an unchecked conversion

Note the lack of "an unboxing conversion" there.

like image 64
Jon Skeet Avatar answered Sep 27 '22 22:09

Jon Skeet