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

  • Home
  • SEARCH
  • 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 5967187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:54:28+00:00 2026-05-22T19:54:28+00:00

I get an error whenever I try to create a simple edge from a

  • 0

I get an error whenever I try to create a simple edge from a node.
Basically, I’ve created two of my own classes called Node and Edge.

The Node class is as follows:

public class Node {

    public String ident;
    public int numLinks;

    public Edge[] neighbours;


    public Node (String ident) {
        this.ident = ident;

    }

    public void setNeighbour (Node start, Node end, int cost, int portNum) {

    }
}

And my Edge class is as follows:

public class Edge {

   Node start;
   Node end;
   int cost;
   int portNum;

   public Edge (Node a, Node b, int cost, int portNum) {
       this.start = a;
       this.end = b;
       this.cost = cost;
       this.portNum = portNum;
   }
}

In my main class, I create two nodes, namely, the start and the end node. The cost and the port number (port number that both these nodes listen to on), I read from a text file and am saving them into an array list named “linkCostList” and “portNumList”.

Now, since each start node can have more than one edge (I’m basically creating a graph), I’m calling the setNeighbour() method in the following way:

for (int i = 0; i < startNode.numLinks; i++) {
    nextNode = new Node (String name of node I read from text file)
    startNode.setNeighbour (startNode, nextNode, linkCostList.get(i), portNumList.get(i));
}

My setNeighbour method is as follows:

public void setNeighbour (Node start, Node end, int cost, int portNum) {
    for (int i = 0; i < start.numLinks; i++) {
        neighbours[i] = new Edge (start, end, cost, portNum);
    }
}

Whenever I compile it, I get an error of the following sort:

Exception in thread "main" java.lang.NullPointerException
    at Node.setNeighbour(Node.java: *line number*)
    at Start.startlsr(Start.java: *line number*)
    at graph.main(lsr.java: *line number*)
}

I understand that this is a null pointer exception, so, somewhere in that loop, I must be doing something wrong. Could anyone please help me figure it out?

  • 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-22T19:54:29+00:00Added an answer on May 22, 2026 at 7:54 pm

    Have you initialzed neighbours in your Node class? It looks like the exception is from accessing a null array (neighbours[i]).

    Also It looks like the neighbours array will grow/shrink dynamically? In this case, instead of using array, consider using ArrayList so you don’t have to grow neighbours yourself.

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

Sidebar

Related Questions

I get the above error whenever I try and use ActionLink ? I've only
In our application we've run into an error numerous times where we get error
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I get this error on an update panel within a popupControlExtender which is within
I get this error when I do an svn update : Working copy XXXXXXXX
I get this error:- You have an error in your SQL syntax; check the
We get this error in Visual Studio 2005 and TFS very often. Can anyone
i get this error {Method 'System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)' has no supported translation to SQL.}
I get this error message when installing SQL 2005 Analysis services. The cabinet File
I get an error when I compile this code: using System; public struct Vector2

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.