0

I have two class like this:

class test1{
      String var1;
      String var2;
      String var3;
      String var4;
      arrayX[] array; 

      public void create(int size){
             arrayX[] array = new arrayX[size]
      }
}   

class test2{
      String var5;
      String var6;
}

then I have two methods:

void firstMeth(){
    test1[] test1obj = new test1[5];
    for(int q = 0 ; q <= 5 ; q++){
    test1obj[q - 1] = new test1();
    }
}
void secondMeth(){
     test1obj[0].create(5);
     test1obj[0].array[0].var5 = "Hello";
     test1obj[0].array[1].var5 = "Super";
     test1obj[0].array[3].var5 = "night";
}

The problem is in the method "secondMeth" because when I try to make assignment for example: test1obj[0].array[0].var5 = "Hello";

I get an error : Cannot invoke methos getAt() on null object. Have you got any idea what I'm doing wrong? What should I change?

Thanks in advance!

0

1 Answer 1

3
  public void create(int size) {
         arrayX[] array = new arrayX[size];
  }

This creates a local array instead of initializing the array on the object level. To correct it, using

  public void create(int size) {
         this.array = new arrayX[size];
  }

EDIT: To address your second problem, creating an array in Java doesn't fill in each element as an object. An object array in Java, unlike that in C++, is simply an array of pointers. You need to initialize each array element to an object like the below:

  public void create(int size) {
         this.array = new arrayX[size];
         for (int i = 0; i < this.array.length; i++) {
             this.array[i] = new test2();
         }
  }

Also default access level for Java is "package" and is not public. So if your calling class isn't in the same package as your classes, you might need to define var5 and var6 public.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks it helps me but now I get error like this: "Cannot set property 'var5' on null object. I add test1obj[0].array[0] = new test2(); But it doesn't work
Edited to address your 2nd problem
I made all variables in both class as public but I still can't access to this variables from others methods, I can use this variables only in this method where I initialize it. How can I get value of "test1obj[0].array[0].var5" from for example the third method ??
It seems to me you are defining multiple top level classes in 1 source file. Doing so making all classes package access. You need to define your classes as public class MyClass {} and define them in multiple source files.
All classes and methods are define and use in one source file. I change all classes to public, but still I can't get value of for example "test1obj[0].array[0].var5". I get the same error : No such property 'test1obj'. All variables in class are public. Have you got any idea what I am doing wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.