Tiberiu Zulean
Tiberiu Zulean

Reputation: 172

Java - Create a list of an array of fixed 40 elements

Sorry if there is a similar question in here, but I have a bit of trouble creating a list (unfixed dimension) of another list or array of fixed dimension (40 elements in this case).

So far I have created a class with a method to add values(string) at a specific position.

public class t_Info_Loan_Class  {
   private String[] t_Info_Loan;

   t_Info_Loan_Class() {
       t_Info_Loan = new String[40];
   }

    private void add(String s, int j) {
        t_Info_Loan[j] = s;
    }
};

Then I tried a simple addition:

t_Info_Loan_Class[] t_Info_Loan_Tab = new t_Info_Loan_Class[40];

   for (int i = 0; i < 2; i++)
      t_Info_Loan_Tab[i] = new t_Info_Loan_Class();

   for(int j = 0; j < 40; j++)
       t_Info_Loan_Tab[0].add("S", j);
   for(int j = 0; j < 40; j++)
       t_Info_Loan_Tab[1].add("D", j);

   for(int i = 0; i < 2; i++) {
      for(int j = 0; j < 40; j++)
         System.out.print(t_Info_Loan_Tab[i].t_Info_Loan[j] + " ");
      System.out.println();
   }

From this, I get a NullPointerException. My intention is to generate:

S S S ... S  (40 times)
D D D ... D  (40 times)

Thanks!

Upvotes: 0

Views: 434

Answers (1)

payloc91
payloc91

Reputation: 3809

You are initializing the array of t_Info_Loan_Class, but you are not initializing the objects themselves.

Before calling any member method, you should create your instances

for (int i = 0; i < 40; i++)
    t_Info_Loan_Tab[i] = new t_Info_Loan_Tab("string");

Upvotes: 2

Related Questions