I have made a series of programs, and I am facing a similar problem. This however is very strange, and maybe if you can get me around this I can find a way through the other ones:
Look at this code:

outside loop:

int iter=0;

loop:
{
blah
blah
--
                                if (iter ==0){
                                temp= P;
                                }
                                System.out.println ("This is iteration number: " + iter);
                                System.out.println ("The top of temp is: " + temp.top ().k1 + ";" + temp.top ().k2 + ";" + temp.top ().k3 + ";");
--blah blah
--
iter ++;
}

The Output of this program is:

At first, This is iteration number 0
The top of temp is 7; 6; 1

At next, This is iteration number 1
Null Pointer Exception: Empty Stack temp

-----------------------------------------------------------------------
Urgently needed, a way out of this trouble. All my methods are static

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You're assigning temp to something (no idea what P is, but it's an assignment) in the case that iter ==0. What is temp in the case that iter !=0? I suspect you're declaring temp and not assigning anything to it except on the first time through.

OK. I need to elaborate a bit.
P and temp are stacks.
I initialized temp outside the loop as
stack temp= new stack ();

I even tried the case with if (iter >0 ) {continue;} else {temp= P;}

didnt work

so, temp should get the value same as P in the first iteration, and in the further iterations retain the value, but apparently the value of temp changes with the value of P.

(P should be null as per my program when iter= 1)

But I don't think the temp should be updated with null, because when iter=0,
temp is assigned a value which should hold constant,as in other values of iter, I have asked it to continue.

Why is temp getting updated?

I suppose there is something to do with Static. This all is inside the Main Method.
In another class, with a static method I encountered a problem (yet not resolved):

public static int [] method (int [] A) {
-----------
print (A);
return B;
}
And A was printed not to be same as what it was initially, but rather same as B

Temp isn't getting updated. It's pointing to whatever P is pointing at.

You need to understand how references to objects work. Nutshell time:

temp does not contain a Stack. Temp contains the address of a stack, which lives somewhere on the heap. When you set temp = p, you're saying "the number stored in temp = the number stored in p". Now temp points to the same address on the heap that p points to. If you change p - say, you paint it green - temp now points to something which is green. If you then change p to be null, temp is pointing to something which is now null.

Things are a little different with Strings, because Java treats Strings a little special, but this is how it works for all other objects.

Hope that answers your question.

I would say that was a pretty useful reply.
How can i get around this? I mean, how can i make temp= P, after only iteration 1 and not always?
do I need to make a method like makesequal (stack P){ } ? If yes, can you give me a hint on how to implement it?

Even in the case of Arrays, the input was updated to answer by the end of the execution. Is that also related to it? I don't think so?

Actually, I'm wrong on an important point - trying to post while working.
If P = some stack
and temp = P

setting P = null shouldn't make temp = null, since you're changing P (the variable on the stack) not the stack on the heap that P points to. Imprecision abounds here, but just to let you know that my answer was not entirely correct.

I'll try to be more clear when I'm not so busy, unless someone chimes in with a better explanation first.

Well, I hope you know you confused me more than before.

Why did temp obtain the value null when P get the value null? Apparently, the previous explanation was insufficient. Then what is it, and what is an intelligent way to get around it?

Yeah, I respect your busi-ness and your will to answer to this thread. Looking forward to your reply, since no one else gave another reply.

Yeah, sorry about that. I just realized that I had a mistake there, and I didn't want to send you off with a wrong idea. The good news is, whatever you liked about that answer was correct up until the last step, where I say "if you change P to point to null, temp will point to null. That of course isn't the case. However, if you change what P points to to be null, then temp will be null. Why? Well, temp keeps on pointing at the same thing all the time. If you change where P points to, temp keeps pointing at the same place. But if you reach in through P (or through temp) and change what's at that location, of course they both see the changed object.

So, what I think happened is that you removed the objects from that stack, and didn't put anything back in it. Tell me - when you say "top()" in the code up there, did you perhaps mean "pop()"?

I don't know what "top()" would do, but pop() is the method for pulling the top item off the stack. If there were three on the stack, and you popped three times, you'd have an empty stack, and that would give you that error.

Sound sensible?

Edited 6 Years Ago by jon.kiparsky: n/a

Well, I did something which now almost took away that problem.

I created a stack temp1, and emptied P into it. Then i empty temp1 into P and temp, so that there is no chance by which they can have the same address now. Now temp has the composition I want.

However the problem I have is, I want to traverse the stack without popping out elements. For example, I want the 4th element of the stack without altering the structure of the stack,

This article has been dead for over six months. Start a new discussion instead.