简体   繁体   中英

java.lang.NullPointerException over a SVGPath LinkedList

i create a SVGPath LinkedList however, when i iterate over this list i'm getting java.lang.NullPointerException . It's a simple LinkedList which has a size of 9. here you can see the code below.

public class RiskControllerClass implements Initializable {

    @FXML
    private SVGPath NA_1; // Alaska

    @FXML
    private SVGPath NA_2; // NorthWest_Ter

    @FXML
    private SVGPath NA_3; // Greenland

    @FXML
    private SVGPath NA_4; // Quebec

    @FXML
    private SVGPath NA_5; // Ontario

    @FXML
    private SVGPath NA_6; // Alberta

    @FXML
    private SVGPath NA_7; // E_America

    @FXML
    private SVGPath NA_8; // W_America

    @FXML
    private SVGPath NA_9; // C_America

    private LinkedList<SVGPath> regions = new LinkedList<>();

    private String nodeID;

    public RiskControllerClass() {

        regions.add(NA_1);
        regions.add(NA_2);
        regions.add(NA_3);
        regions.add(NA_4);
        regions.add(NA_5);
        regions.add(NA_6);
        regions.add(NA_7);
        regions.add(NA_8);
        regions.add(NA_9);

        System.out.println("Length : " + regions.size());

                for (Iterator<SVGPath> iter = regions.iterator(); iter.hasNext();) {
                    SVGPath sVGPath = iter.next();

                    System.out.println(sVGPath.getId());
                }

    }

when i try to print just only sVGPath like System.out.print(sVGPath) it prints all the values null . however, when i print the size of list it gives 9 !. how it could be ?. i will appreciate if you can help and thanks anyway.

Short answer: A java.util.LinkedList can contain null entries.

Detailed answer: Your code is in the constructor of the class, so it's virtually impossible that some framework (FXML) could have put values into NA_1 , NA_2 , ..., NA_9 . So you are now adding nine uninitialized fields to your LinkedList . Uninitialized fields in Java are always null . So your list contains 9 elements, all of which are null .

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