Results 1 to 3 of 3
  1. #1
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default Hey! can anyone help me with array of object . i cant find an error (beginner)

    hi, i m receiving error

    Please help me. and do tell me where i was wrong
    Thank you in advance

    Welcome to National Database
    Exception in thread "main" java.lang.NullPointerException
    at Main.main(
    Java Code:
    import java.util.Scanner;
    public class general {
    	String name;
    	String fname;
    	int dob;
    	char sex;
    	public general()
    	public general(String string, String string2, int i, char c) {
    		// TODO Auto-generated constructor stub
    	protected void addgeninfo()
    		Scanner inp = new Scanner(;
    		System.out.println("Please Enter your name:");;
    		System.out.println("Please Enter your Father's name:");;
    		System.out.println("Please Enter your dob(format:DDMMYYYY):");
    		String temp;
    		System.out.println("Please Enter your sex(m/f):");;
    		sex =temp.charAt(0);
    	public static void main(String[] asgc)
    		int totnationals=10;
    		System.out.println("Welcome to National Database");
    		general nationals[] = new general[totnationals];
    		for( int i =0; i <10; i++)

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default Re: Hey! can anyone help me with array of object . i cant find an error (beginner)

    The reason you are getting that error is because you never created the general object.
    Each time you called addgeninfo() you call it on a null point in the array.
    What you would need to do is to
    Java Code:
    Then you can add all the information, create your object and place in the correct spot in the array

    Also i would definitely think about making another class to hold your object and in this class just have the main method. This will allow each one you create have those instance variables.
    Last edited by jhuber151; 07-19-2012 at 03:36 PM.

  3. #3 is offline Senior Member
    Join Date
    Mar 2011
    Rep Power

    Default Re: Hey! can anyone help me with array of object . i cant find an error (beginner)

    Line 41 simply declares an array of size totnationals. But you have not put any objects in that array. So Line 46 throws the error because there is no object at nationals[i]. That object is null, so you can't invoke a method (addgeninfo()) on it.

Similar Threads

  1. Replies: 11
    Last Post: 02-15-2012, 10:13 AM
  2. Converting String Array To Map - Beginner Help
    By NinjaNife in forum New To Java
    Replies: 4
    Last Post: 02-06-2012, 04:55 PM
  3. Need help with beginner java array program??
    By sd1234 in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 08:24 AM
  4. Replies: 3
    Last Post: 01-04-2012, 02:30 PM
  5. Replies: 2
    Last Post: 03-12-2011, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts