-1

I two classes, A and B. I have a method demo() in class A, that creates an array of objects and by using a previous array1 of ints to get a value 'v3' I will create 'array2' of array1 length. And hence create a new object of array2 with parameters v1,v2,v3. I also have some getter/setters in class B for v1/2/3. I then want to access each element like

                   System.out.print(array2[0].getValue1()); 

outside the method or atleast know that it was created successfully,. But I get an error straight away by trying to print out a value, so I'm guessing not having a toString method would be the problem. I have created a variable outside the method as:

         private B[] array2;

I then call in the main:

  B array2 = new B[array1.length];
  demo();
  System.out.print(array2[0].getValue1());

This is the method

public void demo(){
      int v1= 2;
      int v2= 2;
      int v3= 0;
B[] array2 = new B[array1.length];
for(int x = 0; x<array2.length; x++)
{
     v3 = array1[x];





    array1[x] = new B(v1,v2,v3);
    array1[x].setValue2(v1);
    array1[x].setValue2(v2);
    array1[x].setValue3(v3);


    v1++;
}

} It also seems I have to restate

   B[] array2 = new B[array1.length];

in the method or I get an error. This seems like a similar problem

mint_x
  • 9
  • 3
  • What is the error that you get? – Thilo Oct 08 '17 at 04:37
  • @Thilo I get a exception in the thread "main" java.lang.NullPointerException – mint_x Oct 08 '17 at 04:39
  • 1
    You get that because all elements of `array2` are `null`. Where do you have code that tries to update that array? – Thilo Oct 08 '17 at 04:41
  • @Thilo I do that in the demo() method above, and I'm not updating array2, I try to create array2 there – mint_x Oct 08 '17 at 04:44
  • You are creating another `array2` inside of `demo`, but a) this is completely unrelated to the other one (every method gets their own local variables) and b) you are not updating that one either. – Thilo Oct 08 '17 at 04:52
  • The usual way to share data with methods is to pass it in as an argument and/or receiving it as a return value. – Thilo Oct 08 '17 at 04:54

1 Answers1

1

Basically you have two B[] array2 variables created, one outside demo() method and another inside it.

Since you have created a new B[] array2 variable in demo() method, it is not updating the variable created earlier outside the demo() method.

Because this array (B[] array2 that was created outside) was not modified ever since it was created, so when you try to access the element inside it, NullpointerException is thrown.

Fix: In demo() method don't create a new array again, instead refer the one created outside it. So in demo() method at line 4 it should be like this.

array2 = new B[array1.length];

Hope that helps.

gaurav sk
  • 46
  • 1
  • 6
  • @mint_x Very well. Can you accept the answer by checking the checkmark (`✔`) next to the answer? Read about this etiquette here: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – TT. Oct 08 '17 at 06:53