File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes whats wrong with my code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "whats wrong with my code ?" Watch "whats wrong with my code ?" New topic
Author

whats wrong with my code ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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: 4461
    
    8

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: 692

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: 4461
    
    8

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: 11475
    
  16

They clearly are different objects.

I added this line



right after your line 41. It prints "false".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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: 4461
    
    8

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: 4461
    
    8

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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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 8 API documentation
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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: 4461
    
    8

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: 692

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats wrong with my code ?