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

The Archive Base Latest Questions

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

I have a class that has an ArrayList attribute, and another class that has

  • 0

I have a class that has an ArrayList attribute, and another class that has a 2D Array of that class:

public class someThingsList {

    private List<someThings> lst;

    public someThingsList () {
        this.lst = new ArrayList<someThings>();
    }
    public addThing(someThings) {
        someThings s = someThings;
        this.lst.add(s);
    }
}

and

public class x implements y {

    private static someThingsList[][] field;

    public x(int h, int w) {
    x.field = new someThingsList[h][w];
    for (someThingsList[] lst1 : x.field)
        for (someThingsList lst2 : lst1)
            lst2 = new someThingsList();
}

and when I have for example a new x object with (2,2) as h and w, and try to do

x.fields[0][0].addThing(thing);

I get a NullPointerException error, any idea why?

  • 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-18T22:12:15+00:00Added an answer on June 18, 2026 at 10:12 pm

    Unfortunately you can’t use enhanced for-each loop with arrays when you want to mutate them. lst2 in your example is just another reference (to null), modifying it won’t modify the original array. Try this:

    for(int i = 0; i < field.length; ++i)
        for(int j = 0; j < field[i].length; ++j)
            field[i][j] = new someThingsList();
    

    or alternatively:

    for(int i = 0; i < h; ++i)
        for(int j = 0; j < w; ++j)
            x.field[i][j] = new someThingsList();
    

    As a matter of fact, this problem is not specific to arrays. Check the following code:

    List<String> strings = //...
    
    for(String s: strings) {
        s += "Bar";
    }
    

    Even though it looks as if you were modifying the original strings list, you are barely modifing temporary s variable that is recreated and ignore after each loop.

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

Sidebar

Related Questions

I have a Class that has a private variable with a public setter/getter function:
I have a class that has a method as follows :- public void setCurrencyCode(List<String>
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have a class, Action, that has a method called doAction(). This method exists
I have one class that has a list of objects of Daemon type. class
I have a class that has a propery List<String> or List<SomeObject> . I get
I have my class that is something like this: public class DBConection extends SimpleJdbcDaoSupport
I have a class called Lookup that has two properties: public class Lookup {
I have a class that has several member classes as attributes. The constructor of

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.