File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Problem using java.util.Stack with SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem using java.util.Stack with SAX" Watch "Problem using java.util.Stack with SAX" New topic

Problem using java.util.Stack with SAX

Chilkosae Cozea

Joined: Mar 19, 2009
Posts: 7

Can it be possible the java.util.Stack empty() function is not returning the correct value.
The probelm is sometime I'm getting java.util.EmptyStackException from poppinout funtion even if I check stack.empty() before calling stack.pop function.

I'm using org.xml.sax.ContentHandler to fetch the first 500 character until the first period(".") in the XML. The code look something like this.

I have this condition in popingout function,.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

No, you'll find your error is the obvious one: you are popping from the stack more than you are pushing onto it.

By the way, if you want people to read your code, you should post it inside the "code" tags. I have to say I didn't read the code you posted; unindented code is too hard to read. You can edit your post by clicking on the "Edit" button.
Chilkosae Cozea

Joined: Mar 19, 2009
Posts: 7
Hi Paul
Thanks for your reply.

Sorry for posting the unformatted code. I was not aware of the standard practise of putting the code in this editor.

As you said I'm popping from the stack more than pushing onto it, does that mean the if condition I put "stack.empty()" before calling the Pop function is not working? In all the scenario where I'm popping out I have put the if condition to check whether stack is empty or not.
Chilkosae Cozea

Joined: Mar 19, 2009
Posts: 7
Any updates ???
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

The simple answer is that Stack.empty() won't be returning incorrect values. You haven't included a standalone test case to reproduce your problem so we can't offer much more help. The only reason I can think for for pop() to fail after empty() returns false is if the stack is shared by multiple threads.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
You appear to be throwing away the information that can help you. I refer to:

Gee, do you think an IOException might be important enough to print a stack trace

I agree. Here's the link:
subject: Problem using java.util.Stack with SAX
It's not a secret anymore!