aspose file tools*
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.Stack" Watch "Exception in thread "main" java.lang.Stack" New topic
Author

Exception in thread "main" java.lang.Stack

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I really am a beginner so please accept my apologies if this is an obvious question.

I have a class called Student. Which I am testing in the main TestStudent. Both of these are in the file saved as TestStudent.java.

I am not trying to do anything clever, but I am just getting my head around HashMaps.

In the main part TestStudent I am obtaining two details about a student. Their Name which is to be used as the key and the Course they are enrolled on. I am then passing these to a Hashmap in the Student class. This is in a for loop.

The program compiles fine. But when I enter only one student name and one student course I then get the Exception in thread "main"java.lang.Stack.

My Code is as follows:



The main TestStudent is
[CODE]
public class TestStudent
{
//This is the main program to get input using Java's JOptionPane class and call the showInputDialog method
public static void main(String [ ]args)
{
String Name; //One of the student names input by user
String Course; //One of the stuent enrolled courses input by user
int NumCt = 0; //The number of student names & enrolled courses
// input by user

//GET DATA INPUT FROM USER
for (int lp = 0; lp<=NumCt; lp++)
{
Name=JOptionPane.showInputDialog("Please Enter Student Name: \nEnter 'Exit' to end program");
Course=JOptionPane.showInputDialog("Please Enter Student Enrolled Course: ");

//EXIT LOOP IF "Exit" HAS BEEN ENTERED
if (Name.compareToIgnoreCase("Exit")==0)
{
break;
}
else
{
Student.put(Name, Course); //Store the name and enrolled course in the student class
System.out.println("The number of values in the Student map are: "+Student.StudentDetails.size( ) );
NumCt++;
}//end if else
}//end for statement
System.exit(0);//this is needed when using JOptionPane
}//end main program
}//end public class Student
/CODE]

Previously I have successfully implemented this using a Dynamic Array i.e. the Array Size was determined at run time however, I am now attempting to do it using a HashMap. But I am struggling to get my head around things.

Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea?

Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn?

Thanking you in advance - much appreciated. (Again, apologies if this is so simple).
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
Hi,

you can hit StackOverflow errors on infinite recursions ... and that's what happens in your code.

Student.put() method should not call Student.put() it should call StudentDetails.put(), it seems you did a kind of spelling mistake !

Your code contains other errors, but as your are learning, I'll let you find them (or ask again here)

Happy learning, Arnaud
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi,
You're not a Computeach student doing the first programming exercise of the Java Stage 3 course by any chance? Just that your problem bears a striking resemblance to that assignment.
That assignment also requires you to develop your own method for calculating a hashcode from the Student name, rather than relying on the HashMap implementation to do the work for you. You will learn a lot more about writing code if you write your own method.
Good luck!

Dun


SCJP 1.4<br />SCWCD (in progress)
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Thank you for your response. However could you divulge a little more on answering the original questions:

"Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea? (Arnald Burlet - thanks for your input here).

Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)

Dun Dagda - many thanks for your response. Yes I am a Computeach student doing the first programming exercise. I understand what the assignment is asking BUT I would like to fully understand HashMap before doing the actual assignment. It just the way I work. Small bits at a time. Are you able to assist with the questions above?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)

No you don't need a loop to fill a HashMap (or an Iterator or KeySet). Basically an array is a sequential list which you add to with a position (the index), so you need to say "Object A goes at position 1 in my list", which would look like this:

A HashMap is a unordered, randomly accessable table which you add to with a key. You need to say "Object A is the Object for 'Maureen', so it goes in the 'Maureen' place holder in the table", or:

Its actually a far easier (and more natural) way of handling this than using two-dimentional arrays. One caveat is that it presumes a one to one relationship between name and course - there is only one "Maureen" placeholder, so there can only be one course for "Maureen" - call map.put( ... ) again and the first value is replaced.

If you have an existing list (possibly an array) of values you want to put into a Map, I'd iterate through the list and call put for each object (as you are sort of doing). Its the most obvious way of doing this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
Sorry, but I'm not that good at english, I'll try to answer your 2nd question, but I'm not sure I understood it well !

Originally posted by Maureen Charlton:
"Firstly, what does the exception in thread "main" java.lang.Stack mean? Is it to do with recursion i.e. including the class Student and main TestStudent together is not such a good idea? (Arnald Burlet - thanks for your input here).

That exception means you eated all the available RAM. (see below)
It has to do with recursion yes, but "including the class Student and main TestStudent together" is not the problem, that's normal use. Your problem is INFINITE recursion! Let me explain that quickly.
Whenever you call a function - like System.out.println() or Student.put() - you need some memory on the stack to save informations. The stack is a special zone of memory, a part of your RAM. Each function call needs some space on the stack and if a function call can't obtain the needed space on the stack, you get a java.lang.StackOverflow exception (the one you got running your program).
Now, how is it your program eated all the stack space ? This is because you call infinitely Student.put() (remember each call need some space on the stack), so after some number of function calls, your stack gets full and you get the exception!

got it ?

Originally posted by Maureen Charlton:
Secondly, with a dynamic array you use a loop to implement the array reference for the data i.e. Name would be Name [0][0 ] the first name and Course would be [0][1] but I'm not so sure I have implemented this for a HashMap i.e. as Name is my Key and Course is my value surely I do not need a loop to increment anything in the hashmap; and if I did is my understanding correct when I think I have to use the Set i.e. Set keys = map.keySet() to get an Iterator and use the iterator to put each of the elements of the set in turn? (A response on this question would be appreciated!)


No, you don't need anything special to put values in a HashMap. The way you do it is perfectly correct.
But I think you have a problem, something like only one name,course in the hashmap ?
If that's the case, check your Student.put() method, your body (after my first correction) should be

And that's where the problem resides ... What are you passing to the put() method ? You are not passing the name and course parameters, your are always ordering the hashmap to store the same Strings ! just remove the "" so that your code looks like :


Arnaud

[edit : ubb hates me today, polishing]
[ October 15, 2004: Message edited by: Arnaud Burlet ]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Paul Sturrock and Arnaud Burlet
THANK YOU!
I really was having problems understanding why one would wish to use a HashMap - seemed simple to use a dimensional array. Things are clearer.

However, a HashMap does seem more complicated to code. Especially when trying to print things out and testing.

To print everything out then i.e. the key and the value I assume you have to do the following:



Seems alot of additional code and effort to me.
Is this worthwhile? Or am I missing something?
I guess I've got hooked into dimensional arrays!
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
I think you are making life a bit too complicated for yourself. To solve this exercise, you do not need to bother about HashMaps, etc. All you need to do is define your Student class properly.
This will be something like:

Once you have your Student class properly defined:
In your Storage class, you can then define an array of Student objects
that will store a reference to each Student object, which in turn will hold the name and course information for each student that you want to store.


This should give you a basic idea to get started, and is the approach I used to pass the assignment when I did it. Don't worry about multidimensional arrays and HashMaps, they are only a distraction at this stage. The key point is that once you have your Student object properly defined, you can make an array out of it.

Good luck with your studies,

Dun Dagda
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hello Dun Dagda,

Thank you very much for your response. Sounds like I have gone way, way to far than what is needed for the actual assignment. However, I've got hooked on finding out about this HashMap now. I'll get there in the end.

Incidently, when you did this assignment. How come you didn't use a dimensional array or a HashMap when you didn't know how many student details to enter? Hence you are unable to initialise it to a size. I noticed above you initialised it to 3 i.e. the number of students you wanted to store?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes Maureen it is a little code-heavy iterating through a HashMap and churning out its contents. Its like this because of what a HashMap is designed for: namely a collection of values to be randomly accessed by key. So sequential access is a little awkward.

You could use Dun Dagda's alternative, except it has two problems. It presumes there is a one to one relationship between Name and Course (as does a HashMap, true), so there still can only be one course for "Maureen". Also it requires you know how big your collection will be before you use it, since arrays are not "growable", unlike Collection objects (Map, List and Set - as you've correctly noticed).
[ October 18, 2004: Message edited by: Paul Sturrock ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I would suggest to rethink your commenting-style.

Look at this:

and that:

and decide what's more readable.
Note, that your comment 'Public members' is wrong.
Sections are used in c++ - in java there is no section-idea.


http://home.arcor.de/hirnstrom/bewerbung
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Hello Stefan,

I note your comment about my comments on Public members being wrong.
I took this straight from my Computeach book. I guess for work I do for them I will have to use it but going forward I will take it out.

Thanks again for all your help. Much appreciated.

Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
I only gave three students as an example. For the actual implementation in my own assignment, I used JOptionPane to prompt the user to enter the number of students they wanted to store.

Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
The other thing I wanted to mention was that you don't need to worry about HashMap and its relatives at this stage. You will find out all about these when you come to the third assignment on the Computeach Java Stage 3 course, The Police Database.

Concentrate on defining your methods and calling them properly, as I see you are being advised to do on another thread in this forum.

DD
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
All,

Many thanks for your help - this thread can now be closed. But I don't know what I need to do for this to happen?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.Stack
 
Similar Threads
JOptionPane and ArraySize not known
Method Hash
toString method and output
Arrays and methods - not getting the results I expected
Arrays: Calling input from other methods and classes