1

I'm having trouble with a programming assignment, its a penplotter, there have been quite a few questions about it already here.

Here's a summary:

The paper sheet is understood to be laid out on a grid with both the x-axis and y-axis running from 0 up to infinity (notionally). All drawings are fully contained in this quadrant and can be assumed to fit on the page. Lines can always be assumed not to be contained in other lines. The pen always starts at the origin (0, 0) but can finish anywhere. Lines are specified by the (x, y) coordinates (in integers) of their end points on the grid. The pen can either draw a line between any two points or move (without drawing anything) in a straight line between any two points. As should be obvious, a line can be drawn in either direction. Since we want to minimize the total time to draw a figure, assume the pen moves at constant speed so that an optimal drawing is one that minimizes the total distance moved by the pen whilst not drawing.

All input will be in a file with a sequence of lines of the following form:

Line between x1 y2 and x2 y2

I'm using this for my input: Line between 0 0 and 2 2

Line between 4 1 and 4 4

Line between 4 4 and 4 7

Line between 2 6 and 4 4

Line between 4 4 and 6 2

Line between 6 6 and 4 4

Line between 2 2 and 4 4

and the edges are being stored into the arraylist. I'm not sure why its returning an NullPointerException

I'm almost done, except the following for loop is returning an exception and I'm not sure why. It aims to get the list of all connected edges in the graph

The exception says: Exception in thread "main" java.lang.NullPointerException at Nodes.getConnectedEdges(Nodes.java:55) which is the for loop line "for (Edges a : lines"

public ArrayList<Edges> getConnectedEdges(ArrayList<Edges> lines) {
    ArrayList<Edges> returnData = new ArrayList<Edges>();
    for (Edges a : lines) {
        if(a.getFromNode() == this ){ // if this node is the from node of that edge
        returnData.add(a);

        }
        // if this node is the to node of that edge
        if(a.getToNode() == this){
            returnData.add(a);
        }
    }   
    return returnData;
}

The on flowing problem would be how to get from say the origin (0,0) to an unconnected point say (2,2)?

Thanks in advance

Cottonbuds
  • 19
  • 3
  • What's the exception, and which line throws it? – Jim Garrison Oct 01 '11 at 21:32
  • The exception says: Nodes.getConnectedEdges(Nodes.java:55) which is the for loop line "for (Edges a : lines" – Cottonbuds Oct 01 '11 at 21:34
  • Add the exception to the post, not in a comment. – Jim Garrison Oct 01 '11 at 21:34
  • Post the stack trace please. You didn't say WHICH exception is thrown. – Jim Garrison Oct 01 '11 at 21:35
  • Assuming it's a NullPointerException, that means the input `lines` is null. – Jim Garrison Oct 01 '11 at 21:36
  • ok, thanks, I'll take a look at it and get back to you – Cottonbuds Oct 01 '11 at 21:37
  • I've checked it over, and I've changed my inputs so that the graph is connected, however it's still throwing the nullpointerexception. Would there be any other reason its throwing the exception? – Cottonbuds Oct 01 '11 at 21:53
  • Connectedness has nothing to do with it. When you invoke `getConnectedEdges()` you are passing in a null value, causing the NPE when you try to use it. – Jim Garrison Oct 01 '11 at 21:57
  • But I call "lines.add(new Edges(from, to));" shouldn't that at least give me one non-null value? – Cottonbuds Oct 01 '11 at 22:00
  • Figured out what was wrong, was my input, thanks loads Jim, but now I have another bunch of problems. How do I plus rep here?! – Cottonbuds Oct 01 '11 at 22:11
  • Nice work Cottonbuds. Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. I suggest to publish the remaing problems in new questions. – Zecas May 23 '12 at 14:34

0 Answers0