Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot push onto a stack

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot push onto a stack

    For some reason, whenever I try to push my objects onto a stack, I get a null pointer exception. This is what I have:

    private Stack<BSTnode<K>> myStack;

    private void preOrderTrav(BSTnode<K> node) {
    if(node != null){
    myStack.push(node); // I can't step past this step. This is where error occurs

    while(!myStack.isEmpty()){
    myStack.pop();
    node = node.getLeft();
    myStack.push(node.getRight());
    myStack.push(node.getLeft());

    }
    }

    ...//more irreverent code down here

    I get this error:
    Exception in thread "main" java.lang.NullPointerException
    at BSTSortedListIterator.preOrderTrav(BSTSortedListIt erator.java:14)

    Does anyone know why I'm unable to push onto my stack?

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot push onto a stack

    Does the code assign myStack a value or is it's value null?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Push Based Mechanism
    By mkarthik90 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 18th, 2012, 12:51 PM
  3. Push Notification Client/Server
    By MistaWizurd in forum Java Applets
    Replies: 7
    Last Post: April 30th, 2011, 01:52 AM
  4. stack
    By ridg18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 12:45 PM
  5. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM