0

Following Question. I have a big amount of ArrayList attributes (basically the same as Kreuzlagerort20kg etc). Instead of initializing them all in the constructor (the part commented out) i'd love to initialize them inside the fillLager() method, making it possible to call the method inside the constructor and have them initialized and filled then. If i do it in the code, i always get a nullpointerexception.

Is it possible and/or sensible to initialize an arraylist inside a method, without getting said nullpointer?

import java.util.ArrayList;
public class Lager {
private ArrayList<Screws> KreuzLagerort20kg,KreuzLagerort50kg;

public Lager(){
        //KreuzLagerort20kg = new ArrayList<Screws>();
        //KreuzLagerort50kg = new ArrayList<Screws>();

        fillLager(1,KreuzLagerort20kg,20);
        fillLager(1,KreuzLagerort50kg,50);
}

public void fillLager(int typ,ArrayList<Screws> lager,double lagerGewicht){

     lager = new ArrayList<Screws>();

     // code that loops through combinations and adds them to the arraylist
    }}}}}}
Shenanigator
  • 17
  • 1
  • 6

2 Answers2

1

You can't call new on a variable passed into a method and still have the calling method refer to the original variable, as Java passes by reference. When you call new X() then there is a new reference and the method which called your other method will not know the variable is now pointing at another reference...

e.g.

public void methodA() {
    String s = new String("AAAAA");
    methodB(s);
    System.out.println(s);
}

public void methodB(String referredString) {
    referredString = new String("BBBBB");
}

calling methodA will print "AAAAA"

You will need to initialise in the constructor, or make the method return the variables you passed in...

public void methodA() {
    String s = new String("AAAAA");
    s = methodB(s);
    System.out.println(s);
}

public String methodB(String referredString) {
    referredString = new String("BBBBB");
    return referredString ;
}

calling methodA will now print "BBBBB"

Alternatively - make the string declared outside of either method and don't pass it around at all... e.g.

String s = new String("AAAAA");
public void methodA() {
    methodB();
    System.out.println(s);
}

public void methodB() {
    s = new String("BBBBB");
}

will also yield "BBBBB"

Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
0

You can do it like this: Instead of normally initializing it (like KreuzLagerort20kg = new ArrayList<Screws>();) in constructor, you do it in fillLager.

import java.util.ArrayList;
public class Lager {
private ArrayList<Screws> KreuzLagerort20kg,KreuzLagerort50kg;

public Lager(){
        //KreuzLagerort20kg = new ArrayList<Screws>();
        //KreuzLagerort50kg = new ArrayList<Screws>();

        fillLager(1, 20);
        fillLager(1, 50);
}

public void fillLager(int typ, int code){
     if (code==20){
         KreuzLagerort20kg = new ArrayList<Screws>();
     }
     if (code==50){
         KreuzLagerort50kg = new ArrayList<Screws>();
     }


     // code that loops through combinations and adds them to the arraylist
    }}}}}}
rafaelc
  • 57,686
  • 15
  • 58
  • 82