简体   繁体   中英

Java InvocationTargetException caused by NullPointerException

I had this program with the exact same source code - but since upgrading to Netbeans 8 and JDK 8, it gives me a InvocationTargetException caused by NullPointerException on this line when the "binOneDragDropped" method is executed (which is a method assigned to a label's on drag dropped in JavaFX):

binOneContentsLabel.setText(binOneNewLabel);

The method "binOneDragDropped" is in the class FirstFitController and is defined as:

@FXML
    private void binOneDragDropped(DragEvent event) {
                // Create dragboard object & FXMLController object
                Dragboard db = event.getDragboard();

                // Make sure success is set to false
                boolean success = false;

                // Declare contents array list
                ArrayList binOneContents = new ArrayList();

                if (db.hasString()) {
                    // Add number to the bin (Array List) - if bin is not full
                    if (binOneHasSpace = false) {
                        mainControllerClass.showDialog("Bin is full.");
                    } else {
                        binOneContents.add(db.getString());
                    }

                    // Go through the array list
                    for (Object s : binOneContents) {
                        binOneNewLabel += s.toString() + ", ";
                    }

                    // Update bin contents label
                    binOneContentsLabel.setText(binOneNewLabel);

                    binOneTotalValue = calculateTotalValue(binOneNewLabel);


                    // Check if bin is full and set boolean to false + change bin colour to red if it is
                    if (binOneTotalValue >= Integer.parseInt(binSize)) {
                        // Set boolean to false
                        binOneHasSpace = false;

                        // Set bin colour to red to show user it's full
                        binOne.setFill(Color.RED);
                    }

                    // Set success boolean to true
                    success = true;
                }

                // Let source know whether string drop was successful
                event.setDropCompleted(success);

                // Stop further propagation
                event.consume();
    }

The "calculateTotalValue" method is just a method that gets a set of number as a string separated by commas (eg "1, 2, 3, 4"), separates them and adds them all up to get a total.

"binOneContentsLabel" is simply just a label in JavaFX. "binOneNewLabel" is a String object initialised to "" at the start of the program (not in the method itself).

I tested all of this and it ran perfectly fine (the purpose of it all is to drag a label onto another label and replace the contents of the first label with the second one). But after I uninstalled Netbeans and installed JDK 8 + downloaded Netbeans 8 and installed it again, the project was there but some of the code was automatically changed. I used a backup I had (made just before I upgraded) and copied all the class codes from the backup and replaced it with the code in the classes of my project in the Netbeans directory.

Any help would be really appreciated Thank you

Thanks brian! I found that it was set to static for some reason. I changed it and it works fine now.

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