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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:08:04+00:00 2026-06-12T18:08:04+00:00

public void createGraph () { int oldFrom = -1; int oldTo = -1; for(int

  • 0
  public void createGraph () {
    int oldFrom = -1;
    int oldTo = -1;
    for(int i = 0; i < edges.size(); i++) {
      EdgeI e = edges.get(i);
      int from = e.from;
      int to = e.to;
      VertexI v = vertices.get(from);
      if (from == oldFrom && to == oldTo){
        vertexWeight wic = v.neighbors.get(v.neighbors.size() - 1);
        wic.w++;
      }
      else {
        v.neighbors.add(new vertexWeight (to, 1));
        oldFrom = from;
        oldTo = to;
      }
    }
  }

neighbors is a public List from VertexI class. w is a public integer from vertexWeight class. edges is a list located in my main class. I keep getting a null pointer exception for this line of code:

v.neighbors.add(new vertexWeight (to, 1));

Tried working on it for around 15 minutes and I didn’t get it to work. What am I messing up on?

java.lang.NullPointerException
    at tester.createGraph(tester.java:60)
    at tester.main(tester.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  • 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-12T18:08:05+00:00Added an answer on June 12, 2026 at 6:08 pm

    Short answer

    Initialize v.neighbors with new ArrayList() in vertices.get().

    Long answer

    Your question omitted a crucial information: How you initialized neighbors. Why is this important?

    See: What is a NullPointerException, and how do I fix it?

    In your case I guessed that either v or neighbors is null during the run of the program. For example vertices.get(from) could return null and v.neighbors won’t work. Or neighbors is null, and v.neighbors.add() won’t work.

    And voilà. You admitted that you set neighbors to null when initializing VertexI.

    The solution is: Initialize with new ArrayList() instead of null.

    If that would not have been possible or you cannot avoid null pointers for some other reason, you can do null pointer checks like this:

    if (v != null && v.neighbors != null) {
        v.neighbors.add(new vertexWeight (to, 1));
    }
    

    This means, don’t add vertices if v or neighbors are null.

    But this is complicated and error-prone. It is easier to avoid null pointers as much as possible. Some would say, avoid them at all costs! Throw an exception instead or return an “empty” object like new ArrayList().

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

Sidebar

Related Questions

public void setPunt(int index){ if(index >= 0 && index < PuntenLijst.size()){ x = 3;
public void PlayforMe() { for (int i=game.size()-1;i>=1;i--) { Card a = game.get(i); Card b
public void checkForCollision () { int headX = cells[0].x; int headY = cells[0].y; int
public void printStars(int level) { for (int one = level; one >= 1; one--)
public void setRange(int which) { if (datelimitsset == 1) { if (startPicker.Value >= endPicker.Value
public void Draw(SpriteBatch theSpriteBatch) { Random rand = new Random(); for(int y = 0;
public void writeToParcel(Parcel parcel, int flags) throws IOException { doSomethingThatThrowsIOException(); } Quoth the compiler:
public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode)
public class Graph { private Node node; public void createGraph() { } private class
public void ScoreFirstBall(int pinsKnockedDown) { if (IsStrike(Frame, pinsKnockedDown)) { Score = X; ScoreMessage =

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.