| Author |
whats wrong with my code ?
|
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
ok . now i understand the this keyword ...but here is another problem while implementing GUI
please explain why it is giving me a runtime error , do read the comment on line 41 or so .
|
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3863
|
|
What is the runtime error? If you give us the error message, and the stack trace, it makes like much simpler.
In terms of this/fi:
- this is a reference to the FirstGui object that go() is being called on (so the one created on line 28)
- fi is a reference to the FirstGui object created on line 41
Did you really intend there to be two of them?
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
Matthew Brown wrote:What is the runtime error? If you give us the error message, and the stack trace, it makes like much simpler.
In terms of this/fi:
- this is a reference to the FirstGui object that go() is being called on (so the one created on line 28)
- fi is a reference to the FirstGui object created on line 41
Did you really intend there to be two of them?
i didnt get you ...
you mean to say that "this " and "fi" are two different things ?
but i guess both refer to the same object ? that is FirstGui
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3863
|
|
No, they refer to two separate objects, which are two instances of FirstGui. FirstGui isn't an object, it's a class.
Every time you go new FirstGui(), you are creating a new instance of that class. And you do that twice.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
They clearly are different objects.
I added this line
right after your line 41. It prints "false".
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
Matthew Brown wrote:No, they refer to two separate objects, which are two instances of FirstGui. FirstGui isn't an object, it's a class.
Every time you go new FirstGui(), you are creating a new instance of that class. And you do that twice.
ya , i know they are two different objects , but they are object of the same class ?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3863
|
|
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.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3863
|
|
Actually, I can see what the error is. You were getting a NullPointerException on line 53, right?
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
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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 ?
why ???
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3863
|
|
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.
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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 ?
|
 |
 |
|
|
subject: whats wrong with my code ?
|
|
|