Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:50:05+00:00 2026-06-02T20:50:05+00:00

I have implemented a very basic Stack in Java which is giving strange errors

  • 0

I have implemented a very basic Stack in Java which is giving strange errors never faced before.
The code is as follows:

public class Stack {
Node top;
int size;

public Stack() {top=null; size=0;}

public int pop() {
    if(top!=null) {
        int item = top.data;
        top = top.next;
        size--;
        return item;
    }
    return -1;
}

public void push(int data) {
    Node t = new Node(data);
    t.next = this.top;
    this.top = t;
    size++;
}

public boolean isEmpty() {
    return size<=0 ;
}

public int getSize() {
    return size;
}

public int peek() {
    return top.data;
}

public void printStack() {
    Node n = this.top;
    int pos = this.getSize();
    while(pos>=0) {
        System.out.println("Position: " + pos + " Element: " + n.data);
        if(pos>0) {
            n = n.next;
        }
        pos--;
    }
}
}

class Node {
public int data;
public Node next;

Node(int d) {data=d; next=null;}

public int getData() {return data;}
}

class Tester {
public static void main(String[] args) {
    Stack s = new Stack();
    s.push(9);s.push(2);s.push(7);s.push(3);s.push(6);s.push(4);s.push(5);
    System.out.println("Size is: " + s.getSize());
    //s.printStack();
    for (int i=0; i<s.getSize(); i++) {
        System.out.print(s.pop()+ " ");
    }
    System.out.println();
}
}

I have tested thoroughly and found that the push operation works perfectly with all the 7 elements being pushed in the correct sequence with proper next/top pointer set.
But, when I try to pop out all the elements, then only it pops the top-4 (5-4-6-3) leaving the other ones.
Then, I tried to perform a printStack with the above method and there it gives random NullPointerException errors as follows:

run:
Position: 7 Element: 5
Position: 6 Element: 4
Position: 5 Element: 6
Position: 4 Element: 3
Exception in thread "main" java.lang.NullPointerException
Position: 3 Element: 7
Position: 2 Element: 2
    at Stack.printStack(Stack.java:58)
Position: 1 Element: 9
    at Tester.main(Stack.java:95)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

These errors doesn’t make sense to me and moreover by introducing some more print statements in push() and printStack() for tracing it starts to throw more random exceptions.
The errors are completely non-deterministic for each run and gives different patterns in different machines.
I have traced one complete run with Netbeans debugger and found no errors!

Your help is highly appreciated!
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:50:07+00:00Added an answer on June 2, 2026 at 8:50 pm

    first in printStack() method:

    while (pos > 0) {
    

    instead of

    while (pos >= 0) {
    

    because your 0 position is always null

    and in main:

    int size = s.getSize();
    for (int i = 0; i < size; i++)
    

    instead of

    for (int i = 0; i < s.getSize(); i++)
    

    because your stack size decreasing with each iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am quite new to Indy, have implemented very basic idTCPServer and IdTCPClient between
I have implemented a very basic and by-the-tutorial Pusher triggers in my django app.
I have implemented a very basic plug-in system as part of a static library
I have implemented lucene for my application and it works very well unless you
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have been struggling with this a while now. I implemented a basic IHttpHandler
I have a very basic Activity at the moment. It creates a GLSurfaceView and
I have the starts of a very basic multi-hreaded web server, it can recieve
I have a very basic understanding of how autoconf and automake work, gathered from
I am developoing a Rails v2.3.2 app. A very basic Rails structure implemented as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.