Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Hey! can anyone help me . i cant find an error (beginner)

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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 general.main(general.java:46)
    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();
    		}
     
     
    	}
    }
    Last edited by fredsilvester93; July 18th, 2012 at 07:54 PM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

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

    A NullPointerException (NPE) means that you are trying to use a variable that has not been initialized yet. The exception will tell you which line of your code is causing the error, and so you'll need to inspect that line, find the offending variable, search in your code and see where you think you might be initializing it but in fact are not.

    Edit: I'd look at that constructor of yours where you think you might be initializing class fields, but in fact are not. You might want to fill in the body of that constructor with a little bit of code.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    i cant find any errr when i remove array it works properly.. bt it is nt working properly whn i create array of object

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

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

    In Java (unlike C++) when you "create an array of objects," it actually just creates an array of reference types for that kind of object; it doesn't actually create the objects. (That concept takes a little getting used to. At least it did for me.)

    Anyhow...

    Try something like the following with your original code. (The error has nothing to do with the constructor.)
            general nationals[] = new general[...whatever...];
     
            for( int i = 0; i < nationals.length; i++) //<---It's better not to hard-code the size of the array as some constant.  Use the length member
            {
                nationals[i] = new general();
                // Now you have an object to deal with
                nationals[i].addgeninfo(); // Or whatever...
    .
    .
    .

    Once you get past the problem with null pointer exception, you can go back and flesh out the constructor so that it does what I think you had in mind.



    Cheers!

    Z
    Last edited by Zaphod_b; July 19th, 2012 at 01:47 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    fredsilvester93 (July 19th, 2012)

Similar Threads

  1. Cannot find Error in Beginner code!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2012, 08:44 PM
  2. Please Help me Find the Error
    By jugalrockz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 18th, 2012, 02:31 AM
  3. help to find the error of coding
    By arunjib in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 27th, 2011, 11:28 AM
  4. can any one plz find the error.
    By shalini_sns in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2011, 05:26 PM
  5. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM