I am having trouble programming a DCEL
in Java.
I have Vertex
, Edges
and Faces
.
The DCEL has three ArrayList
of those classes.
The problem is that when I set some attributes of the Edges, it doesn't change that Edge where it is referenced in the ArrayList and therefore I get NullPointerException
.
For example:
DCEL example = new DCEL();
Edge p = new Edge();
example.getVertex().add(new Vertex(new Point(0, 0), p));
example.getEdges().add(p);
p.setOrigin(new Vertex(new Point(1, 1), null));
After executing I can get the point values which I last set if I print this:
example.getEdges().get(0).getOrigin().getC().getX();
But if I try to get them like this I get the NullPointerException, because it doesn't seem to be affected by the last line, where I set the origin of p
:
example.getVertex().get(0).getIncidentEdge().getOrigin().getC().getX();
Edges have an attribute origin, which is a Vertex (among others). Vertex have a coordinate attribute which is a Point and they also have an Edge which is its incident edge.
I think the problem is there are objects inside objects or something, but I don't know how to solve it.
The DCEL
class:
class DCEL {
private List<Vertex> vertex;
private List<Edge> edges;
private List<Face> faces;
DCEL(List<Vertex> vertex, List<Edge> edges, List<Face> faces) {
this.vertex = new ArrayList<Vertex>(vertex);
this.edges = new ArrayList<Edge>(edges);
this.faces = new ArrayList<Face>(faces);
}
DCEL() {
this.vertex = new ArrayList<Vertex>();
this.edges = new ArrayList<Edge>();
this.faces = new ArrayList<Face>();
}
public List<Vertex> getVertex() {
return this.vertex;
}
public List<Edge> getEdges() {
return this.edges;
}
public List<Face> getFaces() {
return this.faces;
}
}
The Vertex
class:
class Vertex {
private Point c; //coordinates
private Edge incidentEdge;
public Vertex(Point c, Edge incidentEdge) {
this.c = c;
this.incidentEdge = incidentEdge;
}
public Vertex() {
this(null, null);
}
public Point getC() {
return this.c;
}
public Edge getIncidentEdge() {
return this.incidentEdge;
}
}
The Point
class:
class Point {
private int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
The Edge
class:
class Edge {
private Vertex origin;
private Edge twin, previous, next;
private Face face;
public Edge(Vertex origin, Edge twin, Edge previous, Edge next, Face face) {
this.origin = origin;
this.twin = twin;
this.previous = previous;
this.next = next;
this.face = face;
}
public Edge() {
this(null, null, null, null, null);
}
public Vertex getOrigin() {
return this.origin;
}
public void setOrigin(Vertex origin) {
this.origin = origin;
}
public Edge getTwin() {
return this.twin;
}
public void setTwin(Edge twin) {
this.twin = twin;
}
public Edge getPrevious() {
return this.previous;
}
public void setPrevious(Edge previous) {
this.previous = previous;
}
public Edge getNext() {
return this.next;
}
public void setNext(Edge next) {
this.next = next;
}
}