The following code generates this:
java.lang.StackOverflowError
at modelo.Secao.eleitores(Secao.java:67)
And I do not know why.
Look at the code:
ArrayList<Eleitor> eleitores(){
return eleitores();
}
Let's see what this eleitores()
method does:
First, it calls the eleitores()
method, which in turn will call the eleitores()
method, which in turn will call the eleitores()
method, which in turn will call the eleitores()
method, which in turn will call the method eleitores()
, ... BUUUUM! StackOverflowError
.
Proposing a solution should be easy, but I am somewhat uncertain and dubious about doing so because you did not post your entire code from the Secao
class, and preferred to post a drug from an image containing a piece of code, instead of posting the actual code (behavior that everyone in this community unanimously hates) . That way, I can not be sure what else is in your Secao
class that would serve to solve your problem. In addition, the search system will have more difficulty with your question, since the content of images can not be indexed. However, despite all this, I think what you wanted is this:
ArrayList<Eleitor> eleitores() {
return eleitores;
}
The only difference here is that the returned value is not succeeded by the parentheses. The idea here is to return the contents of a variable, not to call a method. This confusion happened because you had given the name of the method with the same name as your variable, which is something that Java allows, but it is a bad programming practice, since it is somewhat confusing that it tends to create problems exactly like this one that you are having. To not suffer from this kind of boredom, I suggest doing this:
public List<Eleitor> getEleitores() {
return eleitores;
}
Here I changed the method name to getEleitores()
, leaving the variable name as eleitores
. That way, if you accidentally use eleitores()
(with parentheses) or getEleitores
(without parentheses), the result will be an obvious compilation error instead of something compiling, but that sucks at the time of rolling. p>
Also, I've put the method as public because I think you've forgotten the modifier and have no interest in using packet visibility. I also changed the return type from ArrayList
to% with% since object-orientation principles say to code for an interface, and with List
instead of List
, your API depends on an interface instead of a particular concrete implementation of it.