I need to create a 2D array that holds the number of family members by the highest numbe of titles of one of the family members, and they input on one line. However, I am having trouble when I call the class in my other code in the main method. It says java.nullpointerexception at setTitles, and I know it has something to do with my arrays, but I don't know how to store the user input in the 2d array for my for loops afterwards, because they are based on the size of my 2D array.

public class Geneology
{
private String[] family;  
private int[][] memberName;
private int size;
private int sizeNo;
private String titles;
private Scanner input = new Scanner(System.in);

public String setTitles()
{
  System.out.printf("\nYou will be entering the titles for your family members."
                      +"\nHow many family members will be entered?"
                      +"\nAlso, enter the highest number of titles of one of the family members: ");
  memberName[size][0] = input.nextInt();
  memberName[0][sizeNo] = input.nextInt();


for(int a = 0; a < memberName[size].length; a++)
{
  System.out.printf("\nEnter name of member %d: ", a+1);
  family[a] = input.nextLine();

  for(int b = 0; b < memberName[sizeNo].length; b++)
  {
    System.out.printf("\nEnter title %d for %s: ", b+1, family[a]);
    titles += input.nextLine();
    input.nextLine();
  }
}
  return titles;
}

I just learned arrays, so please bare with me.

Well, I realized I need to initialize my int size and sizeNo and my arrays plus all my other variables, I think! However, I still need to know how to store the size of what the user inputs into my memberName[][].

ha figured it out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.