有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为我的图形制作addedge函数。我需要将边添加到节点

我想从类顶点将边添加到私有链接列表边。请帮我解这个函数。我得到这个错误:

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

顶点。爪哇

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);
//        }}

边缘。爪哇

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;
    }


}

梅因。爪哇

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) 个答案

  1. # 1 楼答案

    出现此错误是因为在创建Vertex的新实例时,尚未初始化该Vertexedges成员变量。让我们来看看你的主要课程:

    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);
                }
    }
    

    因此,在创建新的Vertex时,需要初始化edges成员变量。我们可以在构造函数中执行此操作:

    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);
        }
    }