0

I want to make a stack in Java without using built in class provided by the util package. I wrote this code but it throws a NullPointerException everytime I run it. I made two classes. The first one that that contains the method and the logic of stack i.e Push and Pop and method for checking empty and full of stack;

private int MaxStack;
private int emptyStack;
public static int top;
private char[] items;

public SimpleStack(int i) {
    // TODO Auto-generated constructor stub
}

public void Stack(int i)
{
    MaxStack=i;
    emptyStack=-1;
    top=emptyStack;
    items=new char[MaxStack];
}
public void Push( char c){

 items[top]=c;
 top++;}
public char Pop(char c){

 return items[top--];}
public boolean full(){

 return top+1==MaxStack;}
public boolean empty(){

return top== emptyStack;}}

The second class contains the main method to run the code:

public static void main(String[] args) throws IOException
{
    // TODO Auto-generated method stub
SimpleStack ab=new SimpleStack(10);
char ch;

while((ch= (char)System.in.read())!='\n')
{   

    if(!ab.full()){
        ab.Push(ch);
        }
        }
while(!ab.empty())
{
System.out.println(ab.Pop(ch));
System.out.println();
}


}
}
4
  • 1
    Where is the exception thrown? What is the simplest test case which produces this error? What do you see when you step through the code in your debugger? Commented Oct 8, 2012 at 12:48
  • 1
    "it throws a NullPointerException everytime I run it" => 1. read the stack trace 2. find the line where the exception is thrown 3. try to find out what could be null on that line. Commented Oct 8, 2012 at 12:48
  • Null pointer exceptions are the easiest things in the world to debug, but only if you can look at the stack dump and see what line of code it's happening on. Since you've given us neither the stack dump nor indicated line numbers on your listing, we can only guess. Commented Oct 8, 2012 at 12:49
  • Well for one, when you create your "SimpleStack" object in your main, it's not doing anything with the parameter of 10 because your SimpleStack constructor isn't doing anything. Commented Oct 8, 2012 at 13:05

3 Answers 3

4

The problem is that your constructor doesn't do anything.

It appears you have another method public void Stack(int i) which should be the constructor instead.

I want to make a stack in Java without using built in class provided by the util package.

Even so, you should read the code for the built in Stack class as you could learn something useful, like using standard formatting and coding conventions.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. I tried your suggestion but it shows ArrayIndexOutOfBoundsException
Ok, Where is the exception thrown? What is the simplest test case which produces this error? What do you see when you step through the code in your debugger?
0

Try moving the logic in Stack(int) to SimpleStack(int)

1 Comment

I moved it and it shows : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at stack.SimpleStack.Push(SimpleStack.java:19) at stack.StackImplement.main(StackImplement.java:18)
0

As others have stated, your constructor isn't doing anything. But another issue I see is that even if you move your logic in Stack(int) to SimpleStack(int), you are still setting your int "top" to equal "emptyStack", which is -1. So now when you push an item onto your array, you are initially trying to push it into items[-1], which is obviously going to throw an error. Carefully step through your code line by line and you will be able to see exactly what is going on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.