Bu dersten kullanıyorsanız:Neden dönüştürülemez bir tür hatası alıyorum?
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];
}
}
Düzgün çalışır ve bunu boolean
hiçbir sorun atayabilirsiniz. Kullanıcıya şifre sorduğumda neden aynı şeyi yapamıyorum?
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
Bildiğim kadarıyla ben her iki durumda da aynı şeyi yapıyorum ama ikinci örnek değil iken ilk örnek iyi derler görebilirsiniz.
Son koddaki hatayı alabilir misiniz? –
@MiguelPrz kodumda idi, 'türetilemez tür, beklenen boole bulunamadı Object' – Logan
@LoganDam: Bu bir derleme zamanı hatası mı? Her iki durumda da aynı derleyiciyi (aynı seçeneklerle) kullanıyor musunuz? –