0

I want to add the edge to the private linked-list edges from the class Vertex. Please help me to solve this function. I get this error:

Exception in thread "main" java.lang.NullPointerException at sample.Vertex.setEdges(Vertex.java:19) at sample.Main.main(Main.java:24)

Vertex.java

package sample;

import java.util.LinkedList;
import java.util.List;


    public class Vertex {
        private String label;
       private LinkedList<Integer> edges;

        public void setLabel(String label) {
            this.label = label;

        }}

//        public void setEdges(Edge e) {
//
//            edges.add(e);
//        }}

Edge.java

public class Edge {

        private Vertex destination;
        private double weight;

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void setDestination(Vertex destination) {
        this.destination = destination;
    }

    public double getWeight() {
        return weight;
    }

    public Vertex getDestination() {
        return destination;
    }


}

Main.java

public class Main {

      //  Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

            public static void main(String[] args) {
                // creating the graph A --1.0--> B
                Vertex n = new Vertex();
                n.setLabel("A");
                Vertex b = new Vertex();
                b.setLabel("B");
                Edge e = new Edge();
                e.setDestination(b);
                e.setWeight(1.0);
//                n.setEdges(e);

                // returns the destination Node of the first Edge
double weight = e.getWeight();
System.out.print(e);
                System.out.print(n);
                // returns the weight of
                // the first Edge
            }


    public static void main2(String[] args) {
        launch(args);
    }
}

1 Answers1

0

You are getting this error because when you create a new instance of Vertex, you have not initialized that Vertex's edges member variable. Let's go through your Main class:

public class Main {


            public static void main(String[] args) {
                Vertex n = new Vertex();  // At this point, n.edges is null because it was never initialized
                n.setLabel("A");
                Vertex b = new Vertex();
                b.setLabel("B");
                Edge e = new Edge();
                e.setDestination(b);
                e.setWeight(1.0);
                n.setEdges(e);  // Now you are attempting to add an edge to a null LinkedList, hence the NullPointerException

                ...
            }

            public static void main2(String[] args) {
                launch(args);
            }
}

So, you need to initialize the edges member variable when you create a new Vertex. We can do this in the constructor:

public class Vertex {
    private String label;
    private LinkedList<Edge> edges;

    // implement a constructor that initializes the `edges` field
    public Vertex() {
        this.edges = new LinkedList<>();
    }

    // It's probably better to call this `addEdge` instead of `setEdges`
    public void addEdge(Edge e) {
        // now, this.edges is not null since it was initialized in the constructor
        this.edges.add(e);
    }
}