0

I cant initialize the array made of object results in all methods. If i dont initialize the array in every method it wont work but by doing this all data entered wont be saved in the array.

public class Processes {
    Results [] list = new Results [1000];
    int counter = 0;

public void secondtest (int number){
    list[counter] = new Results();
    if (number == 42){
        list[counter].test2=false;
        } else{
        list[counter].test2=true;
        }
}
public void thirdtest (int number){
    list[counter] = new Results();
    if (number == 56){
        list[counter].test3=false;
        } else{
        list[counter].test3=true;
        }
}
public void fourthtest (int number){
    list[counter] = new Results();
    if (number == 74){
        list[counter].test4=false;
        } else{
        list[counter].test4=true;
        }
}
    public void fifthtest (int number){
        list[counter] = new Results();
    if (number == 6){
        list[counter].test5=false;
        } else{
        list[counter].test5=true;
        }
    }
  • Each array element defaults to the *default value for the type*. For Objects, this is default is `null`. So yes, you need to put in `new Results()` somewhere (this is not "initializing the array"!!) - this might be in a loop that assigns a dummy/blank result to each element right after `new Results[..]`. In any case, using `first,second,third..` as names indicates an *antipattern* and the code could likely be greatly simplified. – user2246674 Sep 05 '13 at 00:51
  • thanks, im pretty new at programming so sorry about my mistakes... im going to try that. – Pepe Alonso Sep 05 '13 at 00:58
  • nope, it didn't work. Null pointer exception error comes out if i don't give a blank result to every element at the begginning of every method which completely eliminates all previously entered data. – Pepe Alonso Sep 05 '13 at 01:04

0 Answers0