简体   繁体   中英

java hashmap nullpointer outside of netbeans

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM