• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables in Static Methods  RSS feed

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above method is in my main class. The reference variable actorBox is static and refers to a JTextField.

The method is then used in another class, but it throws a NullPointerException. My actorBox Object is instantiated with new and works fine for other methods of the same class. Somehow the variable inside the static method points to null.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you ensuring the static variable is set before the static method is called?
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying different things, but as I am a novice programmer, I am still learning from experince. I will try using abstract, but final did not work. When I use the method getText() on the actorBox reference within the same class, it works of course. I want to get the text in the TextField from the main class and use it in a secondary class. I should be able to do this with a static method.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about your question and I initialized the actorBox before my constructor. The NullPointerException for that method is longer there.
 
Nimit Muni
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Remember...static methods should always call a method which is static.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nimit Muni:
Hello

Remember...static methods should always call a method which is static.


Static methods should not exist and are a defective part of the language.
I think the point you're trying to make is "do not call static methods as instance methods". This includes static method calls on an implicit (or explicit) 'this' reference.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!