0

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;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Ignacio
  • 386
  • 4
  • 19
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Nathan Tuggy Jan 13 '15 at 00:52
  • not sure where prueba is from, but you do prueba.getSemiAristas() for the first execution and prueba.getVertex() for the second execution, they might not return the same object. – Dino Tw Jan 13 '15 at 00:58
  • Can you post the getVertexList() source? I think it might be a readonly getter. – felipecrp Jan 13 '15 at 01:02
  • I know why I get nullPointerException. It is because getIncidentEdge() returns null. But I want to know why it returns null, because I initialized p, so it should't be null. – Ignacio Jan 13 '15 at 01:03
  • I think the readonly getter may be the cause. I ve put it now. – Ignacio Jan 13 '15 at 01:07
  • And what if you call `setOrigen` (shouldn't that be `setOrigin`?) before setting the edge or the vertex? – Elliott Frisch Jan 13 '15 at 01:14
  • 1
    Mhh ... `example.getVertexList()` ... `example.getEdge()` ... `example.getVertex()` ... none of them is in the posted `DCEL` class. This is not very helpful, don't you think? And if you know, that your problem is the method `getIncidentEdge` (in the `Vertice` I guess), then can you please post it as well? – Tom Jan 13 '15 at 01:21
  • Sorry for not attaching all the code. I'm kind of new here. :S – Ignacio Jan 13 '15 at 01:58
  • 1
    Well, if I run the same code as you've posted (with an empty `Face` class, but we don't need it), then I don't get any exception. The output is `1` for both `getX()` calls. Have you omitted some lines from the example code? – Tom Jan 13 '15 at 01:58
  • Thanks. I thought the DCEL was the problem. It has to be another class thas is doing something weird then. – Ignacio Jan 13 '15 at 02:03
  • It is a graphic app in Processing so there are some other classes around. I'll have to look at it in detail. – Ignacio Jan 13 '15 at 02:10
  • Have you gone through the stack trace? Can you post the stack trace? – adamdc78 Jan 13 '15 at 02:12
  • You should split the line `example.getVertex().get(0).getIncidentEdge().getOrigin().getC().getX());` into smaller pieces and write the result of each method into a variable. That way you will find the method that returns `null`. – Tom Jan 13 '15 at 02:13

0 Answers0