Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats wrong with my code ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They clearly are different objects.

I added this line



right after your line 41. It prints "false".
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic