문제

내 계산 경로() 메서드에 null 포인터 예외가 있습니다. 이유를 찾는 데 도움을 주세요.아래는 내 코드이고 내 코드 아래는 내가 가진 출력입니다.

정점 클래스:

class Vertex implements Comparable<Vertex> {
    String name;
    Double minDistance = Double.POSITIVE_INFINITY;
    Vertex previous;
    Edge[] adjacencies;

    public String toString() {
        return name;
    }

    public Vertex(String argName) {
        name = argName;
    }

    @Override
    public int compareTo(Vertex other) {
        return Double.compare(minDistance, other.minDistance);
    }

}

엣지 클래스:

class Edge {
    Double weight;
    Vertex target;

    public Edge(Vertex argTarget, Double argWeight) {
        target = argTarget;
        weight = argWeight;
    }
}

알고리즘 클래스:

public class BellmanFord {
    public static void computePaths(Vertex source, Vertex[] vertices) {
        source.minDistance = 0.0;
        PriorityQueue<Vertex> vq = new PriorityQueue<Vertex>();
        vq.add(source);

        for (int i = 1; i < vertices.length - 1; i++) {

            while (!vq.isEmpty()) {

                Vertex u = vq.poll();
                for (Edge e : u.adjacencies) {
                    Vertex v = e.target;
                    System.out.println(u + " " + e.target);
                    Double weight = e.weight;
                    Double distanceThroughU = u.minDistance + weight;
                    if (distanceThroughU < v.minDistance) {
                        vq.remove(v);
                        v.minDistance = distanceThroughU;
                        v.previous = u;
                        vq.add(v);
                    }
                }
            }
        }
    }

    public static List<Vertex> getShortestPathTo(Vertex target) {
        List<Vertex> path = new ArrayList<Vertex>();
        for (Vertex v = target; v != null; v = v.previous) {
            path.add(v);
        }
        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args) {
        Vertex a = new Vertex("A");
        Vertex b = new Vertex("B");
        Vertex c = new Vertex("C");
        Vertex d = new Vertex("D");
        Vertex e = new Vertex("E");

        a.adjacencies = new Edge[] { new Edge(b, -1.0), new Edge(c, 4.0) };
        b.adjacencies = new Edge[] { new Edge(d, 2.0), new Edge(c, 3.0), new Edge(e, 2.0) };
        d.adjacencies = new Edge[] { new Edge(b, 1.0) };
        e.adjacencies = new Edge[] { new Edge(d, -3.0) };

        Vertex[] vertices = { b, c, d, e };
        computePaths(a, vertices);

        for (Vertex v : vertices) {
            System.out.println(" distance to " + v.toString() + " is " + v.minDistance);
            System.out.println("path is " + getShortestPathTo(v));
        }
    }
}

그리고 여기 내 결과가 있습니다

A B
A C
B D
B C
B E
D B
E D
D B
Exception in thread "main" java.lang.NullPointerException
    at BellmanFord.computePaths(BellmanFord.java:50)
    at BellmanFord.main(BellmanFord.java:106)

그래서 라인 50은 for(Edge e : u.adjacencies) 106번째 줄은 computePaths(a,vertices);

도움이 되었습니까?

해결책

NPE 초기화하지 않았기 때문에 발생해야 합니다. adjacencies ~에 c 정점 참조.그러나 당신은 합격했습니다. vertices 배열 computePaths 방법 c.adjacencies ~처럼 null

다른 팁

내 오류였습니다. 내 코드 에서이 코드 라인을 C.Adjacencies= New Edge [] {새 가장자리 (C, 0.0)}로 묶었습니다.1 정점.지금 작동합니다.도움을 주셔서 감사합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top