Reputation: 1439
I'm currently working on a project, that is runned on a GUI by using a JFrame. I want to make as clear as possible what my problem is, i'm working in 3 packages (Entity, Control(Manager), Boundary(GUI)). Boundary imports Control, Control imports Entity.
In the Entity package i got a shape class called "Block" that contains:
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;
}
In the Control package i got a shape class called ShapeCollection This stores the shape in an 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);
}
}
In the control packages i got another class called 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);
}
In the Boundary(GUI) package i got a class called MasterFrame, this has to show a block created by a other frame called BlockFrame in a TextArea.
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);
}
Last class i use in boundary is 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 ();
}
When you press submit button it should make the Block and add it to the 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);
}
When i press the OK button to make a Block as shape i get this error:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
at Control.ShapeController.makeBlock(VormControle.java:25)
at Boundary.BlockFrame.BlokOkButtonActionPerformed(BlockFrame.java:145)
What is causing this NullPointerException?
Upvotes: 2
Views: 159
Reputation: 121998
Consider these methods
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
is nowhere initialized
.
in the constructor
public ShapeController(){
ShapeCollection shapecollection= new ShapeCollection ();
}
You are not actually initializing the instance member. You are initializing a local variable.
Change it to
public ShapeController(){
shapecollection= new ShapeCollection ();
}
Upvotes: 3