Why does this method generate java.lang.Stackoverflowerror?

Asked

Viewed 650 times

-3

The following code generates this:

java.lang.StackOverflowError
    at modelo.Secao.eleitores(Secao.java:67)

And I don’t know why.

Linha do erro

  • Where is line 67??

  • The problem is probably elsewhere. Knowing the error line probably helps to understand where this place is. This error, hidden as it presents itself, is not simple to be solved, and probably occurs because the application architecture is quite wrong. Please read this: http://answall.com/q/95824/101

  • @diegofm The 67 line of the Secao class simply is: Turn voters;

  • OK, I’ll read it. Thanks!

  • 1

    If the code that generates the error is not even present, it cannot answer the question properly. It is a little strange the error run on this line, but it can be. If it is the problem is elsewhere same.

  • 1

    Add a [mcve], so you can simulate your code and the problem.

  • 2

    Are you calling a method within yourself? There’s your problem, eleitores() has no stopping condition, soon will be called endlessly. :)

  • 1

    Put the code in text and not in image, I am editing and taking out everything that has no relation to the problem. my comments were based on the original question that had nothing to do with the problem. Now the question is whether you are calling the same method within it by mistake or whether there is a need to do so. If you have need to resolve the end condition. If you went unintentionally, you have to tidy up and call the correct one. The current way not only gives error, but does not make sense, to be useful should have some state change or at least call other behavior.

  • Putz, there are really two parentheses left on that line. I had to take out and solve one more condition in the setSecaoEleitor method. Victor Stafusa: I had presented an entire method, which was where I called voters(), so I answered my own question with this image, because the staff said they needed line 67, which was where I was accusing the error. Sorry about the noobagem, I don’t know what happened to the question code, I just answered the question with the picture. This site is really great because people intend to help, but the interface.... Thanks guys! *was the first question on the site

Show 4 more comments

1 answer

4


Observe the code:

ArrayList<Eleitor> eleitores(){
    return eleitores();
}

Let’s see what this method eleitores() ago:

First, he calls the method eleitores(), which in turn will call the method eleitores(), which in turn will call the method eleitores(), which in turn will call the method eleitores(), which in turn will call the method eleitores(), ... BUUUUM! StackOverflowError.

Proposing a solution should be something easy, but I’m a little uncertain and doubtful to do so because you didn’t post your entire class code Secao, and preferred to post a damn image containing a piece of code, rather than post the code itself (behavior that everyone in this community unanimously hates). That way, I can’t be sure about what else is in your class Secao that would solve your problem. In addition, the search system will have more difficulty with your question, since the content of images cannot 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 value returned 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’s bad programming practice, since it’s something confusing that tends to create problems exactly like the one you’re having. In order not to suffer from this kind of pain, I suggest we do this:

public List<Eleitor> getEleitores() {
    return eleitores;
}

Here I changed the name of the method to getEleitores(), leaving the variable name as eleitores. That way, if you accidentally use eleitores() (in parentheses) or getEleitores (without parentheses), the result will be an obvious build error instead of something that compiles, but that sucks when running.

Also, I put the method as public because I think you forgot the modifier and have no interest in using the package visibility. I also changed the kind of return ArrayList for List since the principles of object orientation say to encode for an interface, and with List instead of ArrayList, your API depends on an interface rather than a particular concrete implementation of it.

  • I thank all those who tried to help. There are really two parentheses left on line 67 (thanks Renan). I stayed a long time and didn’t even notice, very dumb :s hehe

  • 1

    @Ramnasidharta If this answer helped you and solved the problem, mark it as correct by clicking on the green below the poll numbers. Thank you. :)

Browser other questions tagged

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