You may have known it, but that's not what you said
Anyway, going back to the original problem: as I said, you need to tell us what the error was. If it's a runtime error, it will come with an error message telling us what the problem was, and what line it happened on.
As we've now agreed, you've got two different FirstGui objects. Each of those has a separate button member variable. But only one of those has been initialised. So if you pass in the FirstGui object that doesn't have button set, you'll get an error as soon as you try to set the text on the non-existent button.
Jesper de Jong wrote:But the question is really: why are you creating a second FirstGui object in line 41? What did you want to accomplish by doing that? It does not seem a very logical thing to do.
it is just to see whether " this" and reference variable ("fi ") are same thing or not ...?
which didnt give me any syntax error but run time exceptions and plenty of those ...
and ya @methew its an null pointer exception
but can you explain me in simple language @mathew or any one , why it is giving me a null pointer exception ?
i have created an object and then i have used it ..in line 40 & 41 . so why null pointer exception for this attempt and
no issue , if i write "this " on line 41 ?
I just explained that. You haven't created "an object". You've created two objects.
There are two instances of FirstGui that get created in that code. Each of them have separate instances of the member variable button. These instances get initialised to null. On line 40 you create a JButton, and assign it to one of those variables. The other is still null.
So, if you add the following at the end of the go() method:
you'll see that one of them is null, and the other isn't.
Now, when you click on the button, the actionPerformed method is called on whichever FirstGui object you added as a listener. And that tries to call button.setText(). In one of the cases, button is null there, for the reasons I gave above. In that case you'll get an exception.
The key thing you seem to be missing is that different objects have their own copy of the variables declared within them.
Matthew Brown wrote:I just explained that. You haven't created "an object". You've created two objects.
i have solved it
the problem was actually i was creating the object fi inside the method go() earlier , but that object doesnt contain any button right?
the object that contain button is the one who is calling the go method() , called f ? , earlier not now. right or not mr.methew ?