سؤال

لدي استثناء مؤشر فارغ في طريقة كمبيوتباثس () ، الرجاء مساعدتي في العثور على السبب.أدناه هو رمز بلدي وتحت رمز بلدي هو الإخراج لدي

فئة فيرتكس:

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

نصائح أخرى

لقد كان خطأي ، في الكود الخاص بي ، لقد علقت على هذا السطر من الكود ج. الجوار = حافة جديدة [] {حافة جديدة (ج ، 0.0)} ؛ ، لذلك كانت طريقة الحلقة في مسار الكمبيوتر () أقل من 1 رؤوس.يعمل الآن.شكرا للمساعدة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top