How to use a table of another type of class?


I have declared a class named Member. I then assigned an array with Member type. When I try to put things into the array, it gives me this error:

Exception in thread "main" java.lang.NullPointerException
    at HW2_2.main(

This is my code:

import c.Member;

import java.util.Scanner;
public class HW2_2
     public static void main(String []args)
         Member[] p = new Member[100];
         Scanner in = new Scanner(;

How do I fix this to the point where I can store data into my array?

You have created an object p which points to an array of Member objects. This is perfect. However, each object in your array is by default null. You cannot simply perform operations on them.

You probably want to do something like...

p[0] = new Member(...);
//... And so on

What's important to learn from here is that an array declaration syntax does not initialize the values of the array itself. That would be impossible, right? How would you pass arguments to the constructors of each one seperately? You have to do it manually.