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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:08:43+00:00 2026-06-11T23:08:43+00:00

Im having a problem in a program I’m making and I can’t figure out

  • 0

Im having a problem in a program I’m making and I can’t figure out what the problem is. I have made a couple of smaller test classes to try and figure out what the problem is but I dont get it. I’m sure it’s some basic thing about how Arrays works or something but I can’t seem to remember what. So I post the classes here and hope you guys know whats wrong. Thanks!

public class Main {

    public static void main(String[] args) {
        TestArray t = new TestArray(8);
        t.set(1, 15);
        t.print();
    }

}


public class TestArray {
    private Word[] a;

    public TestArray(int i){
        a = new Word[i];
    }

    public void set(int pos, long value){
        a[pos].set(value);
    }

    public void print(){
        for(Word w : a){
            System.out.println(w);
        }
    }
}



public class Word {
    private long value;

    public Word(long value){
        this.value = value;
    }

    public void set(long value){
        this.value = value;
    }

    public String toString(){
        return String.valueOf(value);
    }
}

It’s when I try to do t.set(1,15) the error occours and Eclipse says something is wrong with the line: a[pos].set(value);

  • 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-11T23:08:45+00:00Added an answer on June 11, 2026 at 11:08 pm

    You aren’t initializing the actual elements of the Word[] a variable. Initialize them if they’re null in your TestArray.set method.

    if (a[pos] == null)
        a[pos] = new Word(value);
    else 
        a[pos].set(value);
    

    The values of an object array in Java are initialized to null, which is contrary to how a primitive array gets initialized (to all zeroes or equivalent thereof). So when you create the array via new Word[i], you’re actually creating an array of null elements, and you have to set them all accordingly.

    Doing it in the set method ensures that you aren’t creating any unused Word objects. This is called lazy initialization. The other way to do it would be to initialize them all to some default value in the constructor:

    public TestArray(int i){
        a = new Word[i];
        for (int index = 0; index < i; index++) {
            a[index] = new Word(0); // Or some other default besides 0, like -1
        }
    }
    

    Per your comment:

    This seems to be the best way but I actually tried to do: (for-each code). Before asking here and that does not work. Why?

    Your code:

    public TestArray(int i) {
        a = new Word[i];
        for(Word w:a) {
            w = new Word(0);
        }
    }
    

    Doesn’t work because w isn’t the actual reference like a[index] is. In a for-each loop on an array, your code actually does this when compiled:

    for (int $i = 0; $i < a.length; $i++) { 
        Word w = a[$i];
        w = new Word(0);
    }
    

    As you can see, you’re assigning a value to the local variable, w, not to the actual element in a, so the array isn’t being changed. One remark: don’t get thrown off by the $ variables because 1) $ is legal in Java variable names (though you should never use them explicitly) and 2) Java generates these variables when it compiles your code (it can be seen in the proper debuggers).

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

Sidebar

Related Questions

I'm having problem when running my Windows Forms program. In the program, I have
I'm having a very odd problem with my program and am hoping you can
all. I'm having a bit of weird problem with client server program. I have
I'm having a problem with a program in which I have to load images
Hey guys im having a problem trying to program out a set of logic.
I am currently having the problem that I have a (partial) program that is
I'm having a problem with a program I'm developing (you can see it at
Im having a problem dynamically loading beans in my program. I have my facelet
I'm having a problem with my program. Basically what i want is, i have
I am having a problem in the program code as I have changed my

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.