我目前正在开发一个项目,该项目使用JFrame在GUI上运行。 我想尽可能清楚地说明我的问题是什么,我正在使用3个包(实体,控制(管理),边界(GUI))。 边界导入Control,Control import Entity。
在Entity包中,我得到了一个名为“Block”的形状类,其中包含:
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);
}
在Boundary(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);
}
当我按下确定按钮使块成为形状时,我收到此错误:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
at Control.ShapeController.makeBlock(VormControle.java:25)
at Boundary.BlockFrame.BlokOkButtonActionPerformed(BlockFrame.java:145)
导致此NullPointerException的原因是什么?
答案 0 :(得分: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 ();
}