Results 1 to 4 of 4
  1. #1
    Pokora is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default Need help correcting - Newbie don't know what's wrong

    So, hello all. This is my first post btw xD Im a first year Computing student and I got suspended so I can't get any more help from my tutors.
    But I still do exercises. This one is supposed to be easy but I'm completely lost.
    I had to develop an application that would store, and then display contacts details from an array.

    I got 2 classes for that. Here's the codes:

    Java Code:
    package addressBook;
    
    public class Person {
    	String firstName;
    	String lastName;
    	String dob;
    	/**
    	 * @param firstName
    	 * @param lastName
    	 * @param dob
    	 */
    	public Person(String firstName, String lastName, String dob) {
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.dob = dob;
    	}
    	/**
    	 * @return the firstName
    	 */
    	public String getFirstName() {
    		return firstName;
    	}
    	/**
    	 * @param firstName the firstName to set
    	 */
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	/**
    	 * @return the lastName
    	 */
    	public String getLastName() {
    		return lastName;
    	}
    	/**
    	 * @param lastName the lastName to set
    	 */
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	/**
    	 * @return the dob
    	 */
    	public String getDob() {
    		return dob;
    	}
    	/**
    	 * @param dob the dob to set
    	 */
    	public void setDob(String dob) {
    		this.dob = dob;
    	}
    	
    	
    
    }
    and

    Java Code:
    package addressBook;
    
    import javax.swing.JOptionPane;
    
    public class AddressBook1 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Person [] contacts = new Person[10];
    		for (int i=0; i < 10; i++) {
    		contacts[i].firstName = JOptionPane.showInputDialog("Input first name");
    		contacts[i].lastName = JOptionPane.showInputDialog("Input last name");
    		contacts[i].dob = JOptionPane.showInputDialog("Input date of birth");
    		System.out.println(contacts[i].getFirstName());
    		System.out.println(contacts[i].getLastName());
    		System.out.println(contacts[i].getDob());
    		}
    
    }
    }
    I'll be thankful for any explanation what did I do wrong, and... don't stone me right away xD

    EDIT: Haha... right ...
    So: It gets to the first entry point (Asking for the first name) and then gives:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at addressBook.AddressBook1.main(AddressBook1.java:13)
    Last edited by Pokora; 02-22-2010 at 04:25 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Pokora View Post
    So, hello all. This is my first post btw xD
    Welcome.


    I'll be thankful for any explanation what did I do wrong, and... don't stone me right away xD
    First it may be a good idea for you to tell us how it's not working.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah... see comments:
    Java Code:
      public static void main(String[] args) {
        Person[] contacts = new Person[10];
        for (int i = 0; i < 10; i++) {
          
          String firstName = JOptionPane.showInputDialog("Input first name");
          String lastName = JOptionPane.showInputDialog("Input last name");
          String dob = JOptionPane.showInputDialog("Input date of birth");
          
          // *** need first to create a Person object for each item of the array:
          contacts[i] = new Person(firstName, lastName, dob);
    
          //.....

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:07 PM
  3. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 03:50 PM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 12:45 PM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 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
  •