• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

NullPointerException..

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check con for null, see below:

 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

My btnSaveListener class.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic