aspose file tools
The moose likes Java in General and the fly likes Variables in Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Variables in Static Methods" Watch "Variables in Static Methods" New topic
Author

Variables in Static Methods

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115


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

Joined: Oct 05, 2001
Posts: 1170

How are you ensuring the static variable is set before the static method is called?
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Oct 03, 2005
Posts: 115
I thought about your question and I initialized the actorBox before my constructor. The NullPointerException for that method is longer there.
Nimit Muni
Greenhorn

Joined: Oct 04, 2005
Posts: 1
Hello

Remember...static methods should always call a method which is static.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Variables in Static Methods
 
Similar Threads
variable in Static Method
Shadowing static variables
Inner Class Failure
Passing a String reference to a method.
static block and static final / non final variables