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

    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(Main.java:13)
    Java Code:
    import java.util.Scanner;
    public class general {
     
    	String name;
    	String fname;
    	int dob;
    	char sex;
    	public general()
    	{
    		this("no","no",0,'n');
    	}
     
    	public general(String string, String string2, int i, char c) {
    		// TODO Auto-generated constructor stub
    	}
     
    	protected void addgeninfo()
    	{
    		Scanner inp = new Scanner(System.in);
     
    		System.out.println("Please Enter your name:");
    		name=inp.next();
     
    		System.out.println("Please Enter your Father's name:");
    		fname=inp.next();
     
    		System.out.println("Please Enter your dob(format:DDMMYYYY):");
    		dob=inp.nextInt();
     
    		String temp;
    		System.out.println("Please Enter your sex(m/f):");
    		temp=inp.next();
    		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++)
    		{
    			nationals[i].addgeninfo();
    		}
     
     
    	}
    }

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

    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:
    addgeninfo(nationals,i)
    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
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    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
  •