• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Implementing stack in java please help

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I am trying to implement stack in java but getting some problems

Here is the class file I wrote



And the main program invoking it



Exception in thread "main" java.lang.NullPointerException
at mypackage.MyStackClass.push(MyStackClass.java:45) === newStack.setData(data);
at mypackage.TestStack.main(TestStack.java:10) === myStack.push(i);

I am a little confused on how to assign the element first time


For some reasons, when I wrote almost the same code in the same class file as main it works(see the code below)




Please help me so that my concepts are clear and I can proceed to implementing other data strcutures
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two approaches to implementing a Stack you've shown are fundamentally different. In your example that "worked", you used an array to store and manage your Stack contents. In your latest attempt, each push creates a new instance of your StackClass. How do you keep track of what's 'in' your stack in the new approach?

As for your error, review your code to determine what 'next' and 'tmpStack.next' are or should be. I'm (easily) confused, but so is the compiler. While you're at it, do the same for 'prev'. I think that's an error in waiting.
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As GB has hinted, a Stack can be implemented atop an array, or a linked lists. Both have advantages and disadvantages. I would suggest you draw the operation of your stack on a sheet of paper. Then you can work out how to implement the push pop peek and isEmpty methods.
Also don't write 222 lines of code and expect it to work. Write the first 5 lines, compile, execute, test, and then try the next 5 lines.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to propose a slightly radical change on your code. As you know, you can only see the top of the stack, so exactly why do you want to contain the stack somewhere?
All you need is to envelop each object on the stack with something that makes it behave like a stack, and a single object that represents the top of said stack:

With these 2 very simple classes you can create any stack you want. To improve on it, use templating. You can add a count to MyStackManager to provide the amount of objects in the stack without having to go through all of the elements and count. Also you do not need to make it static, i am just showing the simplest form of it.
 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I will work on these
 
reply
    Bookmark Topic Watch Topic
  • New Topic