• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with JPasswordField  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm staring at a NullPointerException on reading the contents of the JPasswordField, but can't find the cause (and solution) for it. Do you see it?



 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is not the same passwordTxt attribute.
This one is local to the constructor, and goes out of scope.
Once the constructor has completed the passwordTxt attribute of your class is still null.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
This is not the same passwordTxt attribute.
This one is local to the constructor, and goes out of scope.


You are right. That solved it. When I change 'JPasswordField passwordTxt = new JPasswordField (10);' to 'JPasswordField passwordTxt = new JPasswordField (10);' in the original code it now works as expected. Though why in this example I do the same and get password entered is null still baffles me.

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:. . . When I change 'JPasswordField passwordTxt = new JPasswordField (10);' to 'JPasswordField passwordTxt = new JPasswordField (10);' . . .
And what is the difference between those two?

You should by now know all about the difference between instantiation in the constructor and re‑declaration.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Daan Heuvelbeuk wrote:. . . When I change 'JPasswordField passwordTxt = new JPasswordField (10);' to 'JPasswordField passwordTxt = new JPasswordField (10);' . . .
And what is the difference between those two?

You should by now know all about the difference between instantiation in the constructor and re‑declaration.


Yes. You are right. The second one should have omitted Passwordfield.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second version now sounds correct
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!