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 6340119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:46:38+00:00 2026-05-24T19:46:38+00:00

How to I incorporate NullPointerException in the following push() method for a stack in

  • 0

How to I incorporate NullPointerException in the following push() method for a stack in Java?

public void push(E e) {
        int len = size();
        if (len == 0)
            throw new NullPointerException();
        else
            addElement(e);
        System.out.println("The element pushed is " + e);
    }

In the PSVM, whenever I call the push() method it gives out the NullPointerException without adding to the stack.

public static void main(String[] args) {
        try {
            SortableStack<Object> s = new SortableStack<Object>();
            s.push(10);
            s.push(20);
            System.out.println("The element popped is " + s.pop());
        } 
        catch (NullPointerException e) {
            System.out.println("Null Pointer Exception encountered!");
        }
}
  • 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-05-24T19:46:39+00:00Added an answer on May 24, 2026 at 7:46 pm

    I am not %100 sure what you want to achieve, but you should check for null-ness of e, not the size of the stack:

    public void push(E e) {
        if (e == null) {
            throw new NullPointerException("Can't push a null element");
        }
    
        addElement(e);
        System.out.println("The element pushed is " + e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow this to incorporate spring security in the framework http://java.dzone.com/tips/pathway-acegi-spring-security-
I would like to incorporate scanning for viruses into a Java/Maven/Hudson build process. Unfortunately,
I'm trying to incorporate server-based code diff and highlighting in my GWT (Java) project.
Im looking to incorporate a check functionality in TFS to ensure that new files
I tried to incorporate the following into SSRS but failed. If XXX = A
I'm trying to incorporate some JavaScript unit testing into my automated build process. Currently
I want to incorporate a Flash Pre-loader in my application, but I am using
I wish to incorporate a circular navigation with hover effects on the individual items.
I want to incorporate a minified javascript library (for example http://sizzlejs.com/ ) into my
I'm wanting to incorporate HTML5 database storage into my web application to make it

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.