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

  • SEARCH
  • Home
  • 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 9320367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:37:04+00:00 2026-06-19T03:37:04+00:00

I am new at java and I am trying to write a Linked-List Stack..

  • 0

I am new at java and I am trying to write a Linked-List Stack..

public class Stack {

    private Node first;

    private class Node {
        int item;
        Node next;
    }

    public boolean IsEmpty()
    {
        return first==null;
    }

    public void push(int item)
    {
        Node oldfirst=first;
        first=new Node();
        first.item=item;
        first.next=oldfirst;
    }

    public int pop ()
    {
        int item=first.item;
        first=first.next;
        return item;
    }
}

import javax.swing.*;

public class main {

    public static void main(String[] args) {
        Stack ob=null;
        int num=0;
        while (true)
        {
            num=Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
            ob.push(num);
            if (num==0)
                break;
        }
        int k;
        k=ob.pop();
        JOptionPane.showMessageDialog(null, k);     
    }

now when I enter a number the compiler through
an Execption java.lang.NullPointerException
at main.main(main.java:18)

Why this is happening and how to avoid it
Please be patient and thanks in advance

  • 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-19T03:37:06+00:00Added an answer on June 19, 2026 at 3:37 am

    Your stack ob is null when you call push. You need to instantiate it. Instead of

    Stack ob=null;
    

    you need to have

    Stack ob = new Stack();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I quickly wrote a linked list class in Java. I want to write another
Hey I'm new to java servlets and I am trying to write one that
I am new to java and netbeans. I am trying to write a program
I am trying to write a java ZIP util class as below: package fdbank.util;
I'm new to java and I'm trying to write this code but somehow it
I am new into java world.. But basically I am trying to write a
Guys I'm a bit new in using java and i'm trying to write a
I am trying to write java code to return list of Nodes in a
I'm new to Java and RMI, but I'm trying to write my app in
I am new to Java. I am trying to write something on lines of

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.