2

我目前正在开发一个项目,该项目使用 JFrame 在 GUI 上运行。我想尽可能清楚我的问题是什么,我正在使用 3 个包(实体、控件(管理器)、边界(GUI))。边界导入控制,控制导入实体。

在实体包中,我得到了一个名为“块”的形状类,其中包含:

public class Block extends Shape 
//Shape is the parent of the class Block and a few other shapes
{
private int length, width, height;

public Block(int length, int width, int height){

    this.length= length;
    this.width= width;
    this.height= height; 
}

在 Control 包中,我有一个名为 ShapeCollection 的形状类,它将形状存储在 ArrayList 中

import Entity.*;
import java.util.ArrayList;

public class ShapeCollection{

private ArrayList<Shape> shapecollection;


public ShapeCollection(){
    shapecollection= new ArrayList<Shape>();
}

public void addShape(Shape newShape){
    shapecollection.add(newShape);
}
}

在控制包中,我得到了另一个名为 ShapeController 的类:

//This Controller is making the Shape 

import Entity.*;

public class ShapeController{
private ShapeCollection shapecollection;

public ShapeController(){
    ShapeCollection shapecollection= new ShapeCollection ();
}

public void makeBlock(double length, double width, double height)
{
    Shape shape = new Block( length,  width, height);
    shapecollection.addShape(shape);
}

在边界(GUI)包中,我有一个名为 MasterFrame 的类,它必须显示由 TextArea 中另一个名为 BlockFrame 的框架创建的块。

import Control.*;
public class MasterFrame extends javax.swing.JFrame {

ShapeController shapecontroller;

public MasterFrame () {
    initComponents();
    vormComboBox.removeAllItems();
    vormComboBox.addItem("Select a shape");
    vormComboBox.addItem("Block");
     shapecontroller = new ShapeController ();
 // Here you get referred to a other GUI to make a shape in there, now i use BlockFrame
 // The code behind this works
}

 public static MasterFrame getInstance() {
    return MasterFrameHolder.INSTANCE;
}

private static class MasterFrameHolder{
    private static final MasterFrame INSTANCE = new MasterFrame();

} 

public void addTextBlock(double length, double width, double height) {

    vormcontrole.makeBlock(length, width, height);
    InfoShapeTextArea.setText("Block" + " "+ length + " " + width + "" + height);
}

我在边界中使用的最后一个类是 BlockFrame:

import Boundary.ShapeController;

public class BlockFrame extends javax.swing.JFrame {

   ShapeController shapecontroller;
   private double length;
   private double width;
   private double height;

public BlockFrame() {
    initComponents();

   shapecontroller= new ShapeController ();
}

当您按下提交按钮时,它应该制作块并将其添加到 TextArea:

// The BlockFrame contains 3 textfields to store the doubles in and a submit button

private void BlokOkButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    MasterFrame h = MasterFrame.getInstance();


    length = Double.parseDouble(BlockLengthTextField.getText());
    width = Double.parseDouble(BlockWidthTextField.getText());
    height = Double.parseDouble(BlokHeightTextField.getText());


    shapecontroller.makeBlock(length, width, height);
    h.addTextBlock(length, width, height);
}   

当我按下 OK 按钮将 Block 设置为形状时,我收到此错误:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
at Control.ShapeController.makeBlock(VormControle.java:25)
at Boundary.BlockFrame.BlokOkButtonActionPerformed(BlockFrame.java:145)   

是什么导致了这个 NullPointerException?

4

1 回答 1

3

考虑这些方法

private ShapeCollection shapecollection;

public ShapeController(){
    ShapeCollection shapecollection= new ShapeCollection ();
}

public void makeBlock(double length, double width, double height)
{
    Shape shape = new Block( length,  width, height);
    shapecollection.addShape(shape);
}

shapecollection无处可去initialized

在构造函数中

public ShapeController(){
    ShapeCollection shapecollection= new ShapeCollection ();
}

您实际上并没有初始化实例成员。您正在初始化一个局部变量。

将其更改为

 public ShapeController(){
          shapecollection= new ShapeCollection ();
    }
于 2013-09-18T10:45:40.050 回答