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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:02:07+00:00 2026-06-01T06:02:07+00:00

I’m trying to create a 2 dimensional array of Node objects as follows public

  • 0

I’m trying to create a 2 dimensional array of “Node” objects as follows

    public static void main(String[] args) throws IOException {
            length=getNumber("Enter the length of the field: ");
            breadth=getNumber("Enter the breadth of the filed: ");
            node n = new node();
            node [][] field = new node[length][breadth];
            for(i=0;i<=length;i++){
                for(j=0;j<=breadth;j++){
                    F =getNumber("Enter the F value");
                    field[i][j].setF(F);
                    System.out.println("  "+field[i][j].getF(F);
                }
            }

    }

in above code getNumber is a function wherein i print and accept the number
Here is my node class:

public class node {
public int F;
public int G;
public int H;
public boolean isVisited;
public boolean isCurrent;
public void node(int F,int G,int H,boolean isVisited, boolean isCurrent){
    this.F=F;
    this.G=G;
    this.H=H;
    this.isVisited=isVisited;
    this.isCurrent=isCurrent;

}
public int getF() {
    return G+H;
}
public void setF(int f) {
    F = f;
}
public int getG() {
    return G;
}
public void setG(int g) {
    G = g;
}
public int getH() {
    return H;
}
public void setH(int h) {
    H = h;
}
public boolean isVisited() {
    return isVisited;
}
public void setVisited(boolean isVisited) {
    this.isVisited = isVisited;
}
public boolean isCurrent() {
    return isCurrent;
}
public void setCurrent(boolean isCurrent) {
    this.isCurrent = isCurrent;
}

}

all i want to do is, to store/access various values of F,G,H etc in each of the node objects, the problem however is i’m getting java.lang.NullPointerException for field[i][j].setF(F);
i dont know where i’m going wrong, need some help.

  • 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-01T06:02:08+00:00Added an answer on June 1, 2026 at 6:02 am

    You initialized the array, but you did not populate it.

    Consider this line:

    field[i][j].setF(F);

    When you do

    field[i][j]

    you are accessing the array; i.e. getting what is in the array at that position. Since you didn’t put anything in the array, you get a null. But you immediately try to call setF.

    I noticed you do

    node n = new node();

    outside the loop. You probably want to do that in the loop.

    node n = new node();
    n.setF(F);
    field[i][j] = n;
    

    This code creates a node instance, sets a value on it, and then puts it in the array at the specified position. A bit more fancy approach would be to do something like

    node n = field[i][j];
    if (n == null) { // initialize n at the position if it doesn't exist
      n = new node();
      field[i][j] = n;
    }
    
    field[i][j].setF(f);
    

    Alternatively, you could loop over the array and put a new node at each position, right after you initialize the array.

    Finally, in Java standard practice is to start class names with capital letters. node should be Node.

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

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.