Invoking methods within an Arraylist does not recognize the object method

Asked

Viewed 36 times

-1

Código

Código

I try to invoke the method through the method get().getNome(), but says that the method does not exist.

1 answer

0


Here is the class documentation Object which was the guy you used to store objects in ArrayList. Where is the method getNome() in it? If it has not you can not call, it is not available.

You must say that you stored an object of a more specific type called JavaApplication6 that has this method. But what counts is the type that is specified in the main object. When you give a get(0) the type of return of this method is the type that was declared in creation in the objects placed in perguntas, in accordance with documentation of that method (note that it is generic, so type E there will always be what you declared in the creation of the object. If you don’t understand this, you are doing very advanced things for your present moment of knowledge, go back a little).

Since it is certain that the object you have there is the type you want, could make a cast to transform Object in JavaApplication6. But this is wrong in most applications, will teach you the error, and probably the mistake made is the use of Object when I really wanted to use the JavaApplication6. If you change this the object you will receive will be of this type and has the method you want to use, and everything will be ok.

I would give an example if you hadn’t put the code as an image.

This does not solve possible other problems you are committing in creating the class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.