0

im a biginner in java ,im trying to create a rubber band dynamic graph but when i ran my program it came back with these errors : Exception in thread "main" java.lang.NullPointerException

  • at Node.add_neighbour(Node.java:58)
  • at Graph.add(Graph.java:32)
public class Node {

    private static final double FRICTION = -0.8;
    Vector position;
    Vector velocity;
    Set<Node> Neighbours;
    boolean Mobile, Rigid;

    public Node(Vector vector) {
        Vector vector1 = new Vector(0, 0);
        this.Rigid = false;
        this.Mobile = true;
        Set<Node> myNodes = new HashSet<>();
    }

    public void add_neighbour(Node node) {
        Neighbours.add(node); //**the 1st error is indicated in this line**
    }
}

2nd error:

import java.util.HashSet;
import java.util.Set;

public class Graph {

    Set<Node> nodes = new HashSet<>();

    public Set<Node> getNodes() {
        return nodes;
    }

    public void add(Node node) {
        nodes.add(node);
    }

    public void add(Node node1, Node node2) {
        node1.add_neighbour(node2);//2nd***
        node2.add_neighbour(node1);//2nd****
    }
}    
PajLe
  • 791
  • 1
  • 7
  • 21
  • 1
    `Neighbours` is probably `null` - look through your code - when does `Neighbours` get initialised? I would also encourage you to take look at the [coding conventions for the Java language](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html) - it make it easier for you to understand other people's code and for other people to understand yours – MadProgrammer Mar 09 '20 at 23:04
  • @MadProgrammer true it's not initialised , how can i initialise it into no neighbours knowing that it's type is Set ? – zack__ddx Mar 09 '20 at 23:13
  • One would assume in the same way you did `myNodes` – MadProgrammer Mar 09 '20 at 23:18
  • @MadProgrammer done thanks ,but now it shows another error :**Exception in thread "main" java.lang.OutOfMemoryError: Java heap space** at java.base/java.util.Arrays.copyOf(Arrays.java:3721) at java.base/java.util.Arrays.copyOf(Arrays.java:3690) at java.base/java.util.ArrayList.grow(ArrayList.java:235) at java.base/java.util.ArrayList.grow(ArrayList.java:242) at java.base/java.util.ArrayList.add(ArrayList.java:452) at java.base/java.util.ArrayList.add(ArrayList.java:465) at SVGAnimation.record(SVGAnimation.java:31) at App.main(App.java:16) – zack__ddx Mar 09 '20 at 23:35

0 Answers0