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 807453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:24:02+00:00 2026-05-15T00:24:02+00:00

I am inputting an adjacency list for a graph. There are three columns of

  • 0

I am inputting an adjacency list for a graph. There are three columns of data (vertex, destination, edge) separated by a single space. Here is my implementation so far:

         FileStream in = new FileStream("input1.txt");
         Scanner s = new Scanner(in);

         String buffer;
         String [] line = null;
        while (s.hasNext()) 
        {       
            buffer = s.nextLine();
            line = buffer.split("\\s+");     
            g.add(line[0]);
            System.out.println("Added vertex " + line[0] + ".");
            g.addEdge(line[0], line[1], Integer.parseInt(line[2]));
            System.out.println("Added edge from " + line[0] + " to " + line[1] + " with a weight of " + Integer.parseInt(line[2]) + ".");                  
         }

         System.out.println("Size of graph = " + g.size());

Here is the output:

Added vertex a.
Added edge from a to b with a weight of 9.
Exception in thread "main" java.lang.NullPointerException
    at structure5.GraphListDirected.addEdge(GraphListDirected.java:93)
    at Driver.main(Driver.java:28)

I was under the impression that

 line = buffer.split("\\s+");

would return a 2 dimensional array of Strings to the variable line. It seemed to work the first time but not the second. Any thoughts?

I would also like some feedback on my implementation of this problem. Is there a better way?
Anything to help out a novice! 🙂

EDIT:

I tried this implementation earlier today and was unsuccessful. I did it again here:

FileStream in = new FileStream("input1.txt");
             Scanner s = new Scanner(in).useDelimiter("\\s+");

            while (s.hasNext()) 
            {       
                Scanner line = new Scanner(s.nextLine());
                String vertex = line.next();
                String destination = line.next();
                int weight = line.nextInt();     
                g.add(vertex);
                System.out.println("Added vertex " + vertex + ".");
                g.addEdge(vertex, destination, weight);
                System.out.println("Added edge from " + vertex + " to " + destination + " with a weight of " + weight + ".");                  
             }

             System.out.println("Size of graph = " + g.size()); 

Output:

Added vertex a.
Exception in thread "main" java.lang.NullPointerException
    at structure5.GraphListDirected.addEdge(GraphListDirected.java:93)
    at Driver.main(Driver.java:22)

Edit2:

Here is the addEdge function. This is not my own implementation I am using it to save the time of feebly writing my own at this beginning stage…

package structure5;
import java.util.Iterator;


abstract public class GraphList<V,E> extends AbstractStructure<V> implements Graph<V,E>
{
    protected Map<V,GraphListVertex<V,E>> dict; // label -> vertex

    protected boolean directed; // is graph directed?


    protected GraphList(boolean dir)
    {
        dict = new Hashtable<V,GraphListVertex<V,E>>();
        directed = dir;
    }

    public void add(V label)
    {
        if (dict.containsKey(label)) return; // vertex exists
        GraphListVertex<V,E> v = new GraphListVertex<V,E>(label);
        dict.put(label,v);
    }


    abstract public void addEdge(V v1, V v2, E label);
}

Graph.java:

package structure5;
import java.util.Iterator;

public interface Graph<V,E> extends Structure<V>
{
    public void add(V label);


    public void addEdge(V vtx1, V vtx2, E label);
}

Note: I am leaving out the rest of the methods that don’t pertain to the program

Edit3:
Here is GraphListDirected.java

public class GraphListDirected<V,E> extends GraphList<V,E>
{
    public GraphListDirected()
    {
        super(true);
    }
    public void addEdge(V vLabel1, V vLabel2, E label)
    {
        GraphListVertex<V,E> v1 = dict.get(vLabel1);
        GraphListVertex<V,E> v2 = dict.get(vLabel2);
        Edge<V,E> e = new Edge<V,E>(v1.label(), v2.label(), label, true);   //Line 93
        v1.addEdge(e);
    }
  • 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-05-15T00:24:03+00:00Added an answer on May 15, 2026 at 12:24 am

    String.split returns a String[], a one-dimensional array, never a two dimensional array. You can, of course, further split a String that was the result of a split (and so on).

    Having said that, since you’re already using Scanner, you should never need to use split and Integer.parseInt etc. Just create another Scanner to scan s.nextLine().

    Scanner line = new Scanner(s.nextLine());
    String from = line.next();
    String to = line.next();
    int weight = line.nextInt();
    

    I’m not sure what caused the NullPointerException, although we know from your output that at least one edge was successfully added.

    API links

    • Scanner(String source)
      • Constructs a new Scanner that produces values scanned from the specified string.

    On multiple Scanner

    The little A-ha!! moment here is the realization that just as you can split on the result of a previous split, you can create a Scanner to scan strings returned from another Scanner.

        String inputText =
            "Line1 a b\n" +
            "Line2 d e f\n" +
            "Line3";
        Scanner input = new Scanner(inputText);
        while (input.hasNext()) {
            Scanner line = new Scanner(input.nextLine());
            System.out.println("[" + line.next() + "]");
            while (line.hasNext()) {
                System.out.println("  " + line.next());
            }
        }
    

    The above snippet prints:

    [Line1]
      a
      b
    [Line2]
      d
      e
      f
    [Line3]
    

    This is just a simple example, but you can see the value of this technique when each line is more complex, e.g. when it contains numbers and regex patterns.


    On the NullPointerException

    You need to add both the starting and ending vertices, because of the way addEdge is implemented.

                Scanner line = new Scanner(s.nextLine());
                String vertex = line.next();
                String destination = line.next();
                int weight = line.nextInt();     
                g.add(vertex);
                g.add(destination); // <--- ADD THIS LINE!!
                g.addEdge(vertex, destination, weight);
    

    This should fix the NullPointerException.

    Hopefully the process of finding this bug has been educational: it took several revisions to the question to gather the relevant information, but finally the culprit of the error has been identified.

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

Sidebar

Related Questions

I am inputting data into a mySQL database via a PHP script, but for
Possible Duplicate: iPhone inputting NSUserDefaults into a UITextField Is there a way to remember
Now i am inputting some data from a form and i have a code
I have a form/subform for inputting data into an ado table. The main form's
I am inputting a 200mb file in my application and due to a very
I want my team inputting at least x # of chars into a commit
When a user is inputting something, I need to check user input against the
I am inputting this text file into the command line: 800 5 10 800
I have this form for inputting the birthday in html. But I only have
Hi I have a MVC site and will have users inputting text to be

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.