Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:14:42+00:00 2026-06-18T00:14:42+00:00

Re edit: Sorry if I pasted the code to pastebin. And I don’t know

  • 0

Re edit: Sorry if I pasted the code to pastebin. And I don’t know that you guys could close a question so fast.

So i was modifying the code and after finishing I have no idea why what I did wrong because:

Everytime I add a entry in the map file It instead of calculating the the path It returns infinity on the eigth node: It doesnt give a stack trace but instead it throws:

Distance to Mandurriao: 0.0
Path: [Mandurriao]
Distance to Jaro: 79.0
Path: [Mandurriao, Jaro]
Distance to Iloilo: 118.0
Path: [Mandurriao, Jaro, Iloilo]
Distance to Leganes: 182.0
Path: [Mandurriao, Jaro, Leganes]
Distance to Tagbak: 81.0
Path: [Mandurriao, Tagbak]
Distance to Derp: 214.0
Path: [Mandurriao, Tagbak, Derp]
Distance to Herp: 305.0
Path: [Mandurriao, Tagbak, Derp, Herp]
Distance to Tugigarao: Infinity
Path: [Tugigarao]

And everytime I make my map file less than 7 nodes it gives me:

Exception in thread "main" java.lang.NullPointerException
        at Dijkstra.search(Dijkstra.java:81)
        at Dijkstra.main(Dijkstra.java:132)

I’m a complete java noob, and I have been only making java code like a day ago.

So here’s the code:

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.*;
import java.util.*;

class Vertex implements Comparable<Vertex>
{
    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { name = argName; }
    public String toString() { return name; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }

}


class Edge
{
    public final Vertex target;
    public final double weight;
    public Edge(Vertex argTarget, double argWeight)
    { target = argTarget; weight = argWeight; }
}



public class Dijkstra
{


    public static void computePaths(Vertex source)
    {
        source.minDistance = 0.;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {
        Vertex u = vertexQueue.poll();

            // Visit each edge exiting u
            for (Edge e : u.adjacencies)
            {
                Vertex v = e.target;
                double weight = e.weight;
                double distanceThroughU = u.minDistance + weight;
        if (distanceThroughU < v.minDistance) {
            vertexQueue.remove(v);

            v.minDistance = distanceThroughU ;
            v.previous = u;
            vertexQueue.add(v);
        }
            }
        }
    }

    public static List<Vertex> getShortestPathTo(Vertex target)
    {
        List<Vertex> path = new ArrayList<Vertex>();
        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
            path.add(vertex);

        Collections.reverse(path);
        return path;
    }




     ///////////////////////////////////////////////////////////////////////////

    public static int search(String [ ] numbers, String key)
    {
           for (int index = 0; index < numbers.length; index++)
          {
               if ( numbers[index].equals(key) )
                     return index;  //We found it!!!
          }
         // If we get to the end of the loop, a value has not yet
         // been returned.  We did not find the key in this array.
         return -1;
    }

    ///////////////////////////////////////////////////////////////////////
    public static void main(String[] args)
    {
        int MapLength = 0;
        Vertex[] v = new Vertex[100];
        String[] names = new String[100];
        System.out.println("Initalized");
        try {
            int i = 0;
            System.out.println("Initalized[0]");
            FileInputStream fStream = new FileInputStream("f:\\map2.txt");
            System.out.println("Initalized[1]");
            BufferedReader in = new BufferedReader(new InputStreamReader(fStream));
            System.out.println("Initalized[2]");
            while (in.ready()) {
                String parsed = "";
                parsed = in.readLine();
                String[] t = parsed.split(">");
                if(t[0].equals("v")){
                    System.out.println("Added>" + parsed);
                    v[i] = new Vertex(t[1]);
                    names[i] = t[1];
                    i++;
                    MapLength = i;
                    }

                if(t[0].equals("e")){
                    System.out.println("Initalized[3]");
                    int a;
                    int weight;
                    int ii = 0;

                    System.out.println("Initalized[4]." + t[1]);
                    String[] container = t[1].split("-");
                    System.out.println("Initalized[5]." + container);

                    Edge[] temp = new Edge[(container.length - 1) / 2];


                    for(a = 1; a < container.length; a +=2 ){
                        System.out.println("Initalized[6]." + names[search( names , container[ a ] )] + "...." + Integer.parseInt(container[ a + 1 ]));
                        temp[ii] = new Edge(v[ search( names , container[ a ] ) ], Integer.parseInt(container[ a + 1 ]));
                        ii++;
                        }
                    //System.out.println("Debug@LineCurrentNumberSearch: " +  search( names , container[ a ] ) );
                    v[ search( names , container[ 0 ] ) ].adjacencies = temp;


                    }


            }
            in.close();
        } catch (IOException e) {
            System.out.println("File input error" + e);
            }

/*
    v[0] = new Vertex("Harrisburg");
    v[1] = new Vertex("Baltimore");
    v[2] = new Vertex("Washington");
    v[3] = new Vertex("Philadelphia");
    v[4] = new Vertex("Binghamton");
    v[5] = new Vertex("Allentown");
    v[6] = new Vertex("New York");



    v[0].adjacencies = new Edge[]{ new Edge(v[1],  79.83), new Edge(v[5],  81.15) };

    v[1].adjacencies = new Edge[]{ new Edge(v[0],  79.75),
                                 new Edge(v[2],  39.42),
                                 new Edge(v[3], 103.00) };
    v[2].adjacencies = new Edge[]{ new Edge(v[1],  38.65) };
    v[3].adjacencies = new Edge[]{ new Edge(v[1], 102.53),
                                 new Edge(v[5],  61.44),
                                 new Edge(v[6],  96.79) };
    v[4].adjacencies = new Edge[]{ new Edge(v[5], 133.04) };
    v[5].adjacencies = new Edge[]{ new Edge(v[0],  81.77),
                                 new Edge(v[3],  62.05),
                                 new Edge(v[4], 134.47),
                                 new Edge(v[6],  91.63) };
    v[6].adjacencies = new Edge[]{ new Edge(v[3],  97.24),
                                 new Edge(v[5],  87.94) };
*/

    Vertex[] vertices = new Vertex[MapLength];
    int j;
    for(j = 0; j < MapLength; j++){
        vertices[j] = v[j];
        }

        computePaths(v[0]);
        for (Vertex a : vertices)
    {
        System.out.println("Distance to " + a + ": " + a.minDistance);
        List<Vertex> path = getShortestPathTo(a);
        System.out.println("Path: " + path);
    }
    }
}

and here’s the map file:

v>Mandurriao
v>Jaro
v>Iloilo
v>Leganes
v>Tagbak
v>Derp
v>Herp
v>Tugigarao
e>Mandurriao-Jaro-79-Tagbak-81
e>Jaro-Mandurriao-79-Iloilo-39-Leganes-103
e>Iloilo-Jaro-38
e>Leganes-Jaro-102-Tagbak-61-Derp-69
e>Tagbak-Derp-133
e>Derp-Mandurriao-81-Leganes-62-Tagbak-134-Herp-91
e>Herp-Leganes-97-Derp-87
e>Tugigarao-Herp-100

Can anyone help me? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:14:43+00:00Added an answer on June 18, 2026 at 12:14 am

    If the input file contains the following entry:

    v>Mandurriao
    v>Jaro
    v>Iloilo
    v>Leganes
    v>Tagbak
    v>Derp
    e>Mandurriao-Jaro-79-Tagbak-81
    e>Jaro-Mandurriao-79-Iloilo-39-Leganes-103
    e>Iloilo-Jaro-38
    e>Leganes-Jaro-102-Tagbak-61-Derp-69
    e>Tagbak-Derp-133
    

    The vertex “Derp” does not have any adjacent vertices, hence u.adjacency is null, causing the null pointer exception for entries less than 7.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[edit]: I'm terribly sorry for my timing on asking this question. I've just discovered
Sorry if this question will sound too chaotic, feel free to edit it. I
Edit: I'm sorry, but I forgot to mention that I'll need the values of
If I have the following code (EDIT: Sorry if I wasn't clear, I want
EDIT Sorry I forgot the most important part here. Each key can have more
IMPORTANT EDIT: Sorry everyone, i made a big mistake in the structure. char *name;
EDIT: It does work (sorry). Something in this script is causing it to stop
Sorry for bumpy topic name (feel free to edit if you find more fitting
Edit: I fixed the problem by just starting from scratch. Sorry to waste y'alls
Edit: When I say SQL Server, I'm really talking about the Management Studio. Sorry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.