使用一个程序,该程序具有保存在文件夹和说明文件中的形状库(描述图形的细节)。尝试开发draw()方法时遇到问题(有来自主方法的调用)。 main首先创建9张图纸,然后绘制所有图纸。
draw()被称为NullPointerException时,抛出:

DrawingPanel dp = new DrawingPanel(canvasI.getWidth(), canvasI.getHeight())


为什么?如果您需要更多详细信息,请告诉我

private ArrayList<Shape> allShapes;
private ArrayList<DrawInstruction> allDrawInstructions;
private CanvasInstruction canvasI;
private DrawInstruction drawI;

public Drawing(ShapeLibrary shapeLib, File oneShape) throws FileNotFoundException{
    try{
        allDrawInstructions = new ArrayList<DrawInstruction>();
        Scanner in = new Scanner(oneShape);
        CanvasInstruction canvasI = CanvasInstruction.readFromFile(in);
        while(in.hasNextLine()){
            DrawInstruction drawInstruction = DrawInstruction.readFromFile(in);
            allDrawInstructions.add(drawI);
        }
    } catch(FileNotFoundException e){
        throw new FileNotFoundException("File doesn't exist");
    }
}


public void draw(){
    DrawingPanel dp = new DrawingPanel(canvasI.getWidth(),
    canvasI.getHeight());
}

最佳答案

构造函数中第CanvasInstruction canvasI = CanvasInstruction.readFromFile(in);行的局部变量隐藏
private CanvasInstruction canvasI字段。更改代码如下:-

public Drawing(ShapeLibrary shapeLib, File oneShape) throws FileNotFoundException {
    //...
    Scanner in = new Scanner(oneShape);
    this.canvasI = CanvasInstruction.readFromFile(in);
    //...
}

10-07 19:18