我向数组列表添加形状的方式有什么问题?错误:“线程“JavaFX 应用程序线程”java.lang.NullPointerException 中出现异常”

问题描述 投票:0回答:1

我正在尝试制作一个绘画程序,用户可以在其中绘制形状,我需要稍后从另一个类中的 arraylist 中调用它们。无需尝试添加形状,程序即可正常运行。

这是来自主要 GUI 类“miniPaint”

if (drawButt.isSelected() == true&& shapesCombo.getValue() == "Line") {
            pane.setOnMousePressed(m->{
                Line line = new Line();
                line.setStroke(currentColor.getFill());
                line.setStartX(m.getSceneX());
                line.setStartY(m.getSceneY());
                pane.getChildren().add(line);
            pane.setOnMouseDragged(q ->{
                    line.setEndX(q.getSceneX());
                    line.setEndY(q.getSceneY());
            pane.setOnMouseReleased(a->{
                    collector.addShape(line);
                    });
                });
            });
        }

collector 是另一个类“ShapeCollection”的实例,这是它被使用的部分

public class ShapeCollection {
    private ArrayList<Line> lines;
    private ArrayList<Rectangle> rectangles;
    private ArrayList<Ellipse> ellipses;
    
    public ShapeCollection() {
        ArrayList<Line> lines = new ArrayList<Line>();
        ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
        ArrayList<Ellipse> ellipses = new ArrayList<Ellipse>();
    }
    public void addShape(Shape shape) {
        if (shape instanceof Line) {
            lines.add((Line) shape);
        }
        if (shape instanceof Rectangle) {
            rectangles.add((Rectangle) shape);
        }
        if (shape instanceof Ellipse) {
            ellipses.add((Ellipse) shape);
        }
    }

一旦我写了一行,我就会收到此错误消息(这是开始部分):

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.add(Object)" because "this.lines" is null
    at Project3/application.ShapeCollection.addShape(ShapeCollection.java:22)
    at Project3/application.MiniPaint.lambda$6(MiniPaint.java:98)

我尝试过不通过 addShape() 方法执行此操作,而是直接添加到 arraylist 行,但效果更差

java javafx arraylist shapes
1个回答
0
投票

避免此问题的一种方法是在引用实例变量时使用

this

public class ShapeCollection {
    private ArrayList<Line> lines;
    private ArrayList<Rectangle> rectangles;
    private ArrayList<Ellipse> ellipses;
    
    public ShapeCollection() {
        this.lines = new ArrayList<Line>();
        this.rectangles = new ArrayList<Rectangle>();
        this.ellipses = new ArrayList<Ellipse>();
    }
    public void addShape(Shape shape) {
        if (shape instanceof Line) {
            this.lines.add((Line) shape);
        }
        if (shape instanceof Rectangle) {
            this.rectangles.add((Rectangle) shape);
        }
        if (shape instanceof Ellipse) {
            this.ellipses.add((Ellipse) shape);
        }
    }

查看您的代码,我建议不要使用上面的代码。我认为最好创建变量

final
并继续创建它们。

public class ShapeCollection {
    final private ArrayList<Line> lines = new ArrayList<Line>();
    final private ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
    final private ArrayList<Ellipse> ellipses = new ArrayList<Ellipse>();
    
    public ShapeCollection() {
        
    }

    public void addShape(Shape shape) {
        if (shape instanceof Line) {
            lines.add((Line) shape);
        }
        if (shape instanceof Rectangle) {
            rectangles.add((Rectangle) shape);
        }
        if (shape instanceof Ellipse) {
            ellipses.add((Ellipse) shape);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.