Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in applet

 
jeevansai jinne
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i use variables declared in applet in other class , i tried to implement actionlistener of a button in an applet in other class but i was unable to access and change variable values ,i was getting nullpointer exception.
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether or not you can access a variable depends on the visibility of the variable in question but this isn't a question of whether you can access the variable as that would be a compiler error. Without seeing your code it's impossible to say for sure why you are having a problem but getting an NPE suggests the variable hasn't been set to point to an object yet.
 
jeevansai jinne
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main class is applet class ,extended class is class using main class variables

main class code:



extended class code:


 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
l is null because in the actionPerformed method you have created a new pow object which you are then calling the po() method on. As this new pow object hasn't had init() called on it, 'l' is still null.
What were you trying to achieve?

BTW please use meaningful names and stick to the Java naming conventions as it makes your code so much easier to read. 'l' is a particularly bad name as in many fonts it's hard to distinguish it from '1' and 'i'.
 
jeevansai jinne
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post the modified code by calling init() in extended class by the way l is already initialized in main class
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No for a number of reasons, the main one being that's almost certainly not what you what to be doing. As I asked in my previous post what are you trying to achieve? For instance why do you have a Clock applet that is instantiating a pow applet?
 
jeevansai jinne
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was making timezone converter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic