Creating an array outside of main, get Error "Exception in thread "main" java.lang.StackOverflowErro

I am working through a problem given to us, and currently i am attempting to create an array outside of main, for which the method simstep () can then be run on.
The code is not yet working, but i am getting the above error whenever i run the code. Is there an obvious place i am going wrong?

A stack overflow exception is usually a sign of a non-terminating recursive loop. Look for a line of code that calls the same method that it's in. That's direct recursion. If you can't find any, then look for a line of code that calls one or more methods that eventually call the original method again, without returning first. That's called indirect recursion.

1. On line 198, you create a new instance of TrafficQueue.
2. On object instantiation, it goes through an initialization process. During this process, line 38 is executed.
3. Line 38 creates another instance of TrafficQueue.
4. This again goes through an initialization process. During this process, line 38 is executed.

#3 and #4 is where your non-terminating recursion occurs. This creates as many calls to new TrafficQueue(5) as the JVM stack can handle. Once the stack is maxed out, the JVM will throw a StackOverflow exception.

Quick fix: Don't call new TrafficQueue(5) on line 38.

Your code has a number of other problems though. Once you fix this problem, you'll probably see that your add() method doesn't work.

Well, basically, your TrafficQueue instance has a testQueue instance variable. And this testQueue instance variable is instantiated with a TrafficQueue instance. That TrafficQueue instance, in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance. That another TrafficQueue instance, also in turn, also has a testQueue instance variable. And that testQueue instance variable is instantiated with another TrafficQueue instance.... stack overflow....

1. On line 198, you create a new instance of TrafficQueue.
2. On object instantiation, it goes through an initialization process. During this process, line 38 is executed.
3. Line 38 creates another instance of TrafficQueue.
4. This again goes through an initialization process. During this process, line 38 is executed.

#3 and #4 is where your non-terminating recursion occurs. This creates as many calls to new TrafficQueue(5) as the JVM stack can handle. Once the stack is maxed out, the JVM will throw a StackOverflow exception.

Quick fix: Don't call new TrafficQueue(5) on line 38.

Your code has a number of other problems though. Once you fix this problem, you'll probably see that your add() method doesn't work.

Junilu, Thanks! that makes sense to me!

So my aim was to create a second array of vehicles

and add them (say randomly) to a new queue. This queue i can then run other bits of code on (the simstep() ) for example.

i wanted to create a queue outside of this method so that i could create the queue once, and use it as many times as i wanted.
simstep() will be a piece of code that will go through that queue and perform an action (for instance adding vehicle, removing vehicle, printing vehicle information)

Alex Walton wrote: i wanted to create a queue outside of this method so that i could create the queue once, and use it as many times as i wanted.
simstep() will be a piece of code that will go through that queue and perform an action (for instance adding vehicle, removing vehicle, printing vehicle information)

I hope that makes sense

I think I understand what you're trying to do but, no, it doesn't make sense.

It seems to me you don't have a full grasp of the concept of what an object is and what the static main() method is for. Here's how I like to explain it. See if it makes sense to you.

Java is an object-oriented language. That's why you have classes, which define the general characteristics (fields) and behaviors (methods) of objects, the things that you create with the new keyword. Because everything in Java starts with classes and objects, code that can normally exist independently, like the public static void main() method, still has to be "attached" to something. That "something" is a class. That's why you have code like this:

Notice that the main() method is enclosed in the HelloWorld class. If Java were designed differently (and mind you, I said "If"), we might have been able to write the same program like so:

In fact, in Groovy, another JVM-based language that compiles to Java bytecode, the same program can be written simply as:

Behind the scenes, the Groovy compiler produces practically the same bytecode as the Java HelloWorld example above, albeit with a lot less pomp and ceremony.

The point is, code in your static main() method should be regarded as something that's relatively independent of the concept of objects, fields, and instance methods (methods of a class that are not static). As such, the code in main() are usually high-level "orchestration" commands. They are commands that define the general flow of your program logic. With that in mind, we can write a different version of the HelloWorld program, like this:

The code in main() can now be read/interpreted this way:
Line 7 creates a Greeter object and assigns it to the variable greeter.
Line 8 invokes the greeter.sayHello() method. In plain English, I just say, "Tell the greeter object to sayHello".

Bear with me, I know this answer is taking the scenic route but I want to make sure we're on the same page with regard to how we look at classes, objects, instance methods, and static methods.

In my next post, I'll get to how it relates to what you're doing and how it seems you've got all these concepts jumbled up.

Now, honestly, your code is all over the place. The code's organization is hard to discern, the logical flow is about as streamlined as a haphazardly designed 7-way intersection in a third-world country, and your code's alignment and formatting is about as consistent as the grouping of a drunk sailor shooting a smooth-bore musket.

First, let's look at your main() method. Like I said previously, your main() method should contain high-level orchestration commands. In other words, it should give you a high level view or "story" of what your entire program does. If your program were a book, your main() method would be the blurb on the back cover that gives you a summary of what the whole book is about, except unlike the book blurb, your main() method gives away the whole deal: introduction, setup, climax, who the killer is, and who gets the girl, so to speak. Your main() method should introduce the cast of characters (the classes and object involved) and then make a wide sweep over the entire story arc, from beginning to end.

So, ideally, your main() method would look something like this:

Do you see how very few details there are here? Do you see how the code in the main() method is now a broad sweeping arc of a story? Even though we don't see a lot of details, we kind of get a sense for what this whole program is going to be about, right? Who are the main characters in this story? It's the TrafficQueue and the List of RoadVehicles, right? How hard was it to figure that out? Not very hard, right? Go back to your original code. Did it tell that story in such a direct and easy-to-get manner?

So when you're writing a program, you can think of yourself as a storyteller or even the writer/director of a play or movie. You have control of the script and you have the ultimate say of how the story unfolds. Your cast of characters (classes and objects) are the ones who act out the scenes and say lines and do things. They depend on you to keep the whole production together, to make sure that actors and their roles match and that story line and plot come together logically and coherently. Your current program does not have that quality. Like I said, it's all over the place. It's kind of like a bad movie that was hurriedly spliced together in post production and the scenes are all out of order and jumbled up. It's very difficult to make heads or tails of what's happening.

What you have to do is plan things out a little bit first. When creating large, enterprise scale applications, some teams will spend a day or two drawing up a "storyboard" of what their application is going to do. For your little project, a shorter outline should suffice. So lay out what your TrafficQueue is going to do and how it's going to interact with the list of RoadVehicle objects. Again, start with the high level description. Once you have a 10-thousand foot view, drill down into more details to a 1000-ft view. Then drill down to more details from there, maybe to a 100-foot view.

My approach, and it's one that I've used successfully to stay sane as a programmer, is to try to stay as high-level as long as I can. Then, when I've drilled down enough and broken down the story into the tiniest "scene" as I possibly can, that's where I put in the details. But since the "scene" (method) is so small at that point, I have very little trouble detailing it out.

It goes something like this:

See how I drill down from main() to runSimulationWith() to show() and all those other private static methods? See how little detail there is in those private static methods and how straightforward they are to understand?

Ok, maybe to the untrained eye of a newbie like you, line 25 might not seem very intuitive. This is something that I often do to hide the boolean negation operator, !. Since it's sometimes difficult to make out, I like to write symmetrical pairs of boolean methods like simulationIsOver and simulationIsNotOver. I find the latter easier to read than the equivalent !simulationIsOver() because it's easy to miss the "!" in front of the method call.

So far, all I've shown you is example code that has all static methods. Again, that's because these are all pretty much high-level orchestration commands. They are the outline or "script" of the story. You might be wondering what code goes into the TrafficQueue object itself then. If you go back to the story/play metaphor and think of a TrafficQueue object as a cast member in the play who plays a specific role, it's all the code that allows a TrafficQueue to play out its role.

So, you have to ask yourself: "What role does a TrafficQueue object play in this simulation?" "What things does it 'know'?" "What behaviors does it exhibit?"

From what you've written, you might say:
1. A TrafficQueue object knows which vehicles are "in" it.
2. A TrafficQueue object knows how many vehicles are in it.
3. A TrafficQueue object knows how to add a vehicle to itself.
4. A TrafficQueue object knows how to remove a vehicle from itself.
5. A TrafficQueue object knows if it's full.
6. A TrafficQueue object knows if it's empty.

With just these statements, I think you should be able to figure out appropriate fields and methods that a TrafficQueue object should have. Remember, an object's fields are usually declared as private members, inside the class but outside any of its methods. Instance methods are non-static methods. Keep these fields and methods separate from any static fields and methods you may have declared/written previously.

This is getting quite long I'll stop here for now and let you digest it. But I hope I've given you some ideas on how to better think about your program and how to organize your thoughts and code around these ideas.

Alex Walton wrote: I want to get the queue, so i can perform the code:

i wanted to create a queue outside of this method so that i could create the queue once, and use it as many times as i wanted.
simstep() will be a piece of code that will go through that queue and perform an action (for instance adding vehicle, removing vehicle, printing vehicle information)

I just realized that I almost ended that long-winded journey without bringing it all back to the original question: How do you do what you say you want to do?

I guess my answer is to change the question. Given the ideas that I went over in that series of replies, does the above still sound like a coherent and well-organized description of what you want your program to do? If not, how can you tell your program's story more clearly? What "scene" in your traffic queue simulation story would your simstep() method describe? Right now, this is what it reads like to me:
Scene name: simstep
Summary: ???

Junilu Lacar wrote:. . . the public static void main() method, still has to be "attached" to something. That "something" is a class. . . .

Have you seen the Bunkhouse review of Objects First by Barnes and Kölling? Whoever wrote that said something like, “No procedural‑style HelloWorld programs.”
Obviously that reviewer, as well as Barnes and Kölling, thinks even HelloWorld is so much like introducing procedural coding that it interferes with the process of learning object‑orientation.

Edit: Couldn't find the book on Safari Books Online so fell back to Amazon. Wow, that book is pricey! With a price tag of well over $100, you'd think that the book was written for NASA or something. I guess that doesn't surprise me seeing as how it appears to be a school textbook.

Alex Walton wrote: I want to get the queue, so i can perform the code:

i wanted to create a queue outside of this method so that i could create the queue once, and use it as many times as i wanted.
simstep() will be a piece of code that will go through that queue and perform an action (for instance adding vehicle, removing vehicle, printing vehicle information)

I just realized that I almost ended that long-winded journey without bringing it all back to the original question: How do you do what you say you want to do?

I guess my answer is to change the question. Given the ideas that I went over in that series of replies, does the above still sound like a coherent and well-organized description of what you want your program to do? If not, how can you tell your program's story more clearly? What "scene" in your traffic queue simulation story would your simstep() method describe? Right now, this is what it reads like to me:
Scene name: simstep
Summary: ???