ldam
ldam

Reputation: 4585

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.

Upvotes: 6

Views: 24035

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500385

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.

Upvotes: 8

The problem you have is related with Autoboxing in Java 1.6

You put a primitive type into Object array. Java can not mix primitive with Object, therefore it wrap that primitive boolean into Boolean.

So what you are doing can not be represented as:

boolean result = (boolean) Boolean.TRUE;

The solutions are:

  1. Replace the Object array with boolean array.
  2. Use Boolean.TRUE.equals(result[1]);
  3. Switch to Java 1.7 as John pointed out in his answer.

Upvotes: 1

Biswajit
Biswajit

Reputation: 2506

Use this

 Boolean pair = (Boolean)result[1];

Upvotes: 0

Adam Gent
Adam Gent

Reputation: 49085

Change:

result[1] = (boolean)checkbox.isSelected();

To:

result[1] = Boolean.valueOf(checkbox.isSelected());

Upvotes: 1

Miguel Prz
Miguel Prz

Reputation: 13792

Try to change the boolean by Boolean, which it's a class that inhereits from java.lang.Object and you have the Boolean.TRUE and Boolean.FALSE

Upvotes: 0

Related Questions