java - Initialize Arraylist in method -
this question has answer here:
following question. have big amount of arraylist attributes
(basically same kreuzlagerort20kg
etc). instead of initializing them in constructor (the part commented out) i'd love initialize them inside filllager()
method, making possible call method inside constructor , have them initialized , filled then. if in code, nullpointerexception.
is possible and/or sensible initialize arraylist inside 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 loops through combinations , adds them arraylist }}}}}}
you can this: instead of initializing (like kreuzlagerort20kg = new arraylist<screws>();
) in constructor, 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 loops through combinations , adds them arraylist }}}}}}
Comments
Post a Comment