|
-
Instantiating global object
I've run into a situation that I don't quite understand and was hoping somebody
could help.
I have created a simple class that I'm using in my main class. I tried declaring
a global variable to this class and then instantiate it when Main is called.
Ex:
package myApp;
import myApp.animal;
public class JFrame extends javax.swing.JFrame {
private animal myAnimal;
public static void main(String args[])
{
myAnimal = new animal();
}
private void jButtonMouseClicked()
{
myAnimal.setX(100); // generates an exception error
}
}
In the code above, if you click the button, a null exception is generated.
My guess is because the object is not being instantiated even though I do
so in "main".
If I change the declared variable to read:
private animal myAnimal = new animal();
and delete the instantiation in "main", the code works.
This is confusing to me because I thought you could declare a global class
object and instantiate it elsewhere. For example, I could set "int x;" and
then set its value in a method.
Anyone help on explaining this one is greatly appreciated. Thanks!!!!
Jaime
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks