wood burning stoves*
The moose likes Beginning Java and the fly likes NullPointerException.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException.." Watch "NullPointerException.." New topic
Author

NullPointerException..

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi Folks,

My app finally compiled ( ) and I then launched it.

I filled out all of the fields in my GUI and clicked the 'save' button and was very excited until I got the following error

Any help is greatly appreciated!



Looking at that, I'm pretty sure that the error is in the following line of code as it is line 88 in my btnSaveListener class:


My dbConnect method is as follows:
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
check con for null, see below:



Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
I added it, like you said but I'm still getting the same error.

I wasn't sure if I had to pass something into my dbConnect() method or not?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
John Pisci wrote:I added it, like you said but I'm still getting the same error.

I wasn't sure if I had to pass something into my dbConnect() method or not?


if indeed the problem is with c.dbConnect(); , is it not possible that the object c has not been instantiated?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

John Pisci wrote:I added it, like you said but I'm still getting the same error.

I wasn't sure if I had to pass something into my dbConnect() method or not?


It is not in the dbConnect() method. If it was, the dbConnect() method would be part of the stacktrace. You need to check to see iif the "c" instance is null.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44

I thought I had instantiated it with the "this.c = connect;"

My btnSaveListener class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

John Pisci wrote:
I thought I had instantiated it with the "this.c = connect;"

And how do you construct your instances? Are you sure you're not passing null to the constructor?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Rob Prime wrote:
John Pisci wrote:
I thought I had instantiated it with the "this.c = connect;"

And how do you construct your instances? Are you sure you're not passing null to the constructor?


Would I have to add in "c = new connect()" in the actionPerformed method?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

No, I meant was: how do you create new btnSaveListeners? Because if you pass null to the constructor, then the reference stays null of course.
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Rob Prime wrote:No, I meant was: how do you create new btnSaveListeners? Because if you pass null to the constructor, then the reference stays null of course.


I'd create a new btnSaveListener by typing "btnSaveListener bsl = new btnSaveListener()", but that would still be null, right?

I'm not sure how I would add a value to 'c' without typing something like c = "1" or "test", which then gives me an incompatible types error.

Apologies for this being a simple error, but I just can't get my head around it!

Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

I'd create a new btnSaveListener by typing "btnSaveListener bsl = new btnSaveListener()", but that would still be null, right?


If you did this, then you still have compile errors... please don't run you code until you get rid of all of your compile errors.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

I'm not sure how I would add a value to 'c' without typing something like c = "1" or "test", which then gives me an incompatible types error.

Apologies for this being a simple error, but I just can't get my head around it!


The "connect" class type is part of your program. You wrote it (or got it from somewhere). If you can't figure out how to instantiate one, how do you expect us (who don't even know what it is) to tell you?

Henry
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry Wong wrote:
I'd create a new btnSaveListener by typing "btnSaveListener bsl = new btnSaveListener()", but that would still be null, right?


If you did this, then you still have compile errors... please don't run you code until you get rid of all of your compile errors.

Henry


How do you know you've got rid of all of the compile errors until you have compiled?
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry Wong wrote:
I'm not sure how I would add a value to 'c' without typing something like c = "1" or "test", which then gives me an incompatible types error.

Apologies for this being a simple error, but I just can't get my head around it!


The "connect" class type is part of your program. You wrote it (or got it from somewhere). If you can't figure out how to instantiate one, how do you expect us (who don't even know what it is) to tell you?

Henry


Yes I did write it.

I appreciate that what I asked was basic, but we all start somewhere- you were a beginner once too, so please, try not to patronise me.

I have tried adding "connect c = new connect()" to my actionPerformed method, but I got the 'cannot find symbol' error again. I got past this last time by adding the following, so now I am confused!:


My full connect method:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Yes I did write it.

I appreciate that what I asked was basic, but we all start somewhere- you were a beginner once too, so please, try not to patronise me.


I am *not* patronizing you. I am trying to tell you that we don't know the answer !!

It is not a matter of whether the question is basic or not -- it is a matter of only the person with access to the code (ie. you) can know. We don't have access to the code. Hence, we can't answer the question.

I am confused on how explaining how we can't answer your question is considered patronizing.

Henry

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


Your constructor takes two parameters. To construct a connect object you must use...

c = new connect(blah, blah);

Where the first blah is a reference to a btnSaveListener object, and the second blah is a reference to a gList object.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Your classes also seem to have a circular requirement... Your btnSaveListener class requires a connect object in order to instantiate it (with no other option to instantiate it). And your connect class requires a btnSaveListener object in order to instantiate it (with no other option to instantiate it).

In other words, both classes require that you have an instance of the other first, or you won't be able to create it.

Henry
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry Wong wrote:
Your constructor takes two parameters. To construct a connect object you must use...

c = new connect(blah, blah);

Where the first blah is a reference to a btnSaveListener object, and the second blah is a reference to a gList object.

Henry


Ohhh I seee... It's obvious when you point it out! I'd forgotten about this bit of code, and didn't even click when I pasted similar code earlier...D'Oh!
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry Wong wrote:Your classes also seem to have a circular requirement... Your btnSaveListener class requires a connect object in order to instantiate it (with no other option to instantiate it). And your connect class requires a btnSaveListener object in order to instantiate it (with no other option to instantiate it).

In other words, both classes require that you have an instance of the other first, or you won't be able to create it.

Henry


I hadn't really thought about it this way and now you've pointed it out, it just seems badly written.

Looking at the connect class, there is no need for me to have a btnSaveListener object in there if the gList object is there. I think I may have added the btnSaveListener object in because I thought that I would need to refer to it to set the lblInfo text (it made sense at the time!), despite there being a gList object where I can access lblInfo directly.

I'll have a play with the code when I get back from work, but many thanks for your help
 
GeeCON Prague 2014
 
subject: NullPointerException..