Calling Main Class method returns NullPointer

Hi guys,

I'm working on a multi-files project in Java. This should be run from the main method of "Main.java" class (no command line parameters needed). This class creates a new instance of a "Platform" object (succesfully!!) .

Then the class has a get method which should return the same as above "Platform" object..

Here is the code of the main

privatestatic MyPlatform myplatform;

publicstaticvoid main (String args[]){

//crea piattaforma

myplatform=new MyPlatform();

}

publicsynchronizedstatic MyPlatform getMyPlatform(){

return myplatform;

}

}

The get method is called from another class in the SAME PACKAGE,

but I keep on getting NULLPOINTER EXCEPTION when I call this method...

Code of the other class ("Initiator") :

protectedstatic MyPlatform platf;

public Initiator(){

try{

platf=Main.getMyPlatform();

if(Main.getMyPlatform()==null)thrownew Exception("PLATFORM NOT FOUND");

}catch(Exception e ){

System.out.println("Warn");

e.printStackTrace();

}

}

platf is always null : It works only when I add at the beginning of the second class : Main.main(null); so when I create a new Instance of the main class....

but I shouldn't....

Moreover I can't change the code structure...

Anybody knows how to solve this problem?

Thanks a lot!!

Bye

[2441 byte] By [madcowzza] at [2007-11-26 18:54:56]
# 1

> platf is always null : It works only when I add at

> the beginning of the second class : Main.main(null);

> so when I create a new Instance of the main

> class....

You don't create an instance of Main. You just call a method on it.

> Moreover I can't change the code structure...

> Anybody knows how to solve this problem?

Huh?

Anyway, your problem is that the code in your MyPlatform.main(String[]) method is obviously never executed.

CeciNEstPasUnProgrammeura at 2007-7-9 20:32:32 > top of Java-index,Java Essentials,Java Programming...
# 2
Make a public class called Main and put the contents of Main.java in there.
Lord_Jirachia at 2007-7-9 20:32:32 > top of Java-index,Java Essentials,Java Programming...
# 3
Though intialization of static variables should happen in the static block anyway.static { myPlatform = new MyPlatform();}
CeciNEstPasUnProgrammeura at 2007-7-9 20:32:32 > top of Java-index,Java Essentials,Java Programming...
# 4
// Or simply private static MyPlatform myplatform = new MyPlatform ();
BIJ001a at 2007-7-9 20:32:32 > top of Java-index,Java Essentials,Java Programming...
# 5
Rather than main(), I think maybe you actually want to use static initializers.Although I'd also suggest taking a step back and asking yourself whether there should be so much static code.
paulcwa at 2007-7-9 20:32:32 > top of Java-index,Java Essentials,Java Programming...