Error in Java Exception in thread "main" java.lang.RuntimeException

0

I have as code:

//Get e-mail server 
@Override
public String getType(){
   return(String) typeComboBox.getSelectedItem();
}

I see an error in those lines and I do not know what it could be, apart from the fact that in the first one (In "get e-mail server") it asks me to put the Override and when I put it, it asks me to remove it and it is the only one error that marks, any idea why it will be?

    
asked by Chivo Alvarado 08.08.2017 в 08:52
source

1 answer

0

Of the comments:

  

the error appears in'public String getType () {'appears as "getType () is ConnectDialog can not override getType () in Wondow return type String in not compatible with Type" and "Add Override Anotation"

They are two different things.

This comes because you are overwriting a method of the parent class (or, from Java 7, implementing a method of an interface).

The "Add Override Annotation" is just an advice; to make sure that "marks" for the compiler that the method implements a method of the parent / interface class 1 . It has nothing to do with the error.

The error is that, when implementing the method, you are doing it in a way that you define a return type that has nothing to do with the original method. For example, even the original method has a signature public int getType() . As there is no automatic conversion possible from String to int , that definition does not allow 2 .

Solution: Make the return type match the one defined in the superclass / interface.

1 What this does is force the compiler to check that, indeed, the method exists in the superclass / interface. That way, if you change the signature of the method there, it warns you to change the subclass, or warns you if you have misspelled the signature.

2 If in the superclass / interface you had defined the method as public Object getType() then there would be no error, since a String is a subclass of Object and you could assign the String where getType() is used.

    
answered by 08.08.2017 в 09:30