2

I have the following code in netbeans (using javafx in the same project):

public class ExperimentControler {
    public static HashMap<String,Double> userInput = null;
    public static ObservableMapWrapper<String,Double> userInputObservable = null;   
}

and

static final String totalDistance = "Total distance";
public static void main(String[] args) {
    ExperimentControler.userInput = new HashMap<String,Double>();
    ExperimentControler.userInput.put(totalDistance, 300.0);
    ExperimentControler.userInputObservable = new ObservableMapWrapper<String,Double>(ExperimentControler.userInput);

    Application.launch(PhysicsGui.class, args);
}

@Override
public void start(Stage primaryStage) {
    ExperimentControler.userInput.get(totalDistance);
    //...
}

This is working perfectly inside netbeans.
If I "clean and build" the project, the resulted .jar file throws a null pointer exception on this line:

ExperimentControler.userInput.get(totalDistance);

Also, this is my java version outside of netbeans:

>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)

I also tried with jre 1.7.0 but the results were exactly the same..
In netbeans I have jdk 1.6.0_26.

Stefanos Kalantzis
  • 1,619
  • 15
  • 23

2 Answers2

2

OK... thanks to Kal's comment:

How are you running this program? Have you tried putting System.out.printlns() in your main method to make sure that they are called before the app crashes with a NPE?

I figured out that the following (javafx) code (must be this.. there is no other entry point):

@Override
public void start(Stage primaryStage) {

bypasses the main() when I run it as standalone. Maybe the root cause is totally different I don't know..
The fact is that in netbeans, main() is running and on the standalone is not..
I also checked the jar's manifest and the main-class is correct. (just in case)

My mind could not go to the fact that main is not running at all !
So, I moved the code I had in main() to the overrided start method and it works.

The specification says that the start() method is the main entry point for javafx applications. But, in my understanding, main() should still be called before start().. this could be a bug on javafx.

Stefanos Kalantzis
  • 1,619
  • 15
  • 23
  • What command do you use to start the program? It's not possible bypass the main method when starting a Java application on the command line - the only possibility is that it's calling some other main method than the one you think it is. Are you perhaps developing an applet? – Esko Luontola Aug 09 '11 at 23:08
  • I have tried "java -jar file.jar" and double click the .jar // There is no other main method. – Stefanos Kalantzis Aug 09 '11 at 23:28
  • What about if you remove the call to `Application.launch()` from the main method? Then it should stop starting, which means that the main method is indeed being called. If that is the case, then there is still the possibility of multiple class loaders confusing things. – Esko Luontola Aug 10 '11 at 04:57
  • I removed the Application.launch() from main() and the application runs as standalone but not within netbeans! – Stefanos Kalantzis Aug 10 '11 at 07:25
  • Can you put the JAR somewhere for others to download and try? – Esko Luontola Aug 10 '11 at 21:08
-3

I had something like this before.

Hashmap auto-boxing might be the problem here. I think you are trying to autounbox a null value.

Try

ExperimentControler.userInput.put(totalDistance, new Double(300.0));

\EDIT OK thanks @hovercraft, If this doesn't work, you must be storing a null in your hash map somewhere else in your code. Remember that get(totalDistance) is replaced by get(totalDistance).doubleValue(); if you are assigning to an double.

As to why it doesn't work out of the jar... no idea sorry.

PS What is the exact line for ExperimentControler.userInput.get(totalDistance);? are you assigning it to an Double or a double? that can make all the difference.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • The autoboxing should work in the original code, so this is not the issue. And you had a NullPointerException due to autoboxing problems? Really? – Hovercraft Full Of Eels Aug 09 '11 at 21:57
  • yes. if you put null into the hashmap, `get` will try to autounbox and throw NPE. check the docs if you don't believe me! – Sanjay Manohar Aug 09 '11 at 22:00
  • but he's not putting null in. Again his put and get work without your modification. If you don't believe me, check. And of course you'll have problems if you put null into a hashmap, but I still don't see how autoboxing issues figure in. – Hovercraft Full Of Eels Aug 09 '11 at 22:02
  • Besides, his problem is that either the userInput HashMap is null or the ExperimentControler is null (but that's a class, so not likely), not that it contains null values. That's the only reason a NPE will be thrown at the line indicated. – Hovercraft Full Of Eels Aug 09 '11 at 22:07