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 2 of 2

Thread: Array of strings Null Pointer error

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array of strings Null Pointer error

    My error reads
    Exception in thread "main" java.lang.NullPointerException
    at Phonebook.main(Phonebook.java:41)

    I also know this error occurs again at lines 45 and 49

    here is my code
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
     
    public class Phonebook {
     
    class Entry{
    	public String Name, Number, Notes; 
    }
     
    public static Entry[] ContactList = new Entry[100];
    public static java.io.File PhoneBookEntries = new java.io.File("PhoneBookEntries.txt");
     
    public static void main(String[] args) throws FileNotFoundException{
    	Scanner input = new Scanner(System.in);
    	java.io.File PhoneBookEntries = new java.io.File("PhoneBookEntries.txt");
    //	Entry[]ContactList = new Entry [100];
    	java.io.PrintWriter output = new java.io.PrintWriter(PhoneBookEntries);
    	String tempName;
    	Character Decision;
    	int count;
    	int length;
    ///	for(int i = 0; i < 100; i++  ){//
    		System.out.println("Codes are printed as 1 to 8 characters \n use \"e\" for enter," +
    						   "\"f\" for find, \"l\" to list and \"q\" to quit.");
     
    	tempName = null;	
    	count = 0;
    	while(count < 100){
    		System.out.print("Command: ");
    		tempName = input.nextLine();
    		Decision = tempName.charAt(0);
     
    		switch(Decision){
    		case 'e' :  System.out.print("Enter Number: ");
     
     
    					ContactList[count].Name = input.nextLine();
     
     
    					System.out.print("Notes: ");
    					ContactList[count].Notes = input.nextLine();
     
    					length = tempName.length();
    					tempName = tempName.substring(2, length);
    					ContactList[count].Name = tempName;
     
    					StorePhoneBook(ContactList, count);
    					break;
     
    		case 'E' :	System.out.print("Enter Number: ");
    					ContactList[count].Number = input.nextLine();
    					System.out.print("Notes: ");
    					ContactList[count].Notes = input.nextLine();
     
    					length = tempName.length();
    					tempName = tempName.substring(2, length);
    					ContactList[count].Name = tempName;
     
    					StorePhoneBook(ContactList, count);
    					break;
     
    		case 'f' :  ReadPhoneBook(PhoneBookEntries);		
    					break;
     
    		case 'F' :  ReadPhoneBook(PhoneBookEntries);
    					break;
     
    		case 's' :  SortList(PhoneBookEntries);
    					break;
     
    		case 'S' :  SortList(PhoneBookEntries);
    					break;
     
    		case 'l' :  ListAllContacts(PhoneBookEntries);
    					break;
     
    		case 'L' :	ListAllContacts(PhoneBookEntries);
    					break;
    		}
     
    	count++;
    	}
     
    }
     
    public static void ReadPhoneBook(File PhoneBoookEntries) throws FileNotFoundException{
     
    }
     
     
    public static void StorePhoneBook(Entry ContactList[], int count) throws FileNotFoundException{
    	java.io.PrintWriter output = new java.io.PrintWriter(PhoneBookEntries);
     
    	output.print(ContactList[count]);
    }
     
     
    public static void ListAllContacts(File Phonebook)throws FileNotFoundException{
     
    }
     
    public static void SortList(File PhoneBook)throws FileNotFoundException {
     
    }
     
    }
    Last edited by copeg; April 2nd, 2011 at 08:57 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array of strings Null Pointer error

    The first problem is that you've commented out the line declaring ContactList.
    //	Entry[] ContactList = new Entry [100];

    Secondly, in Java arrays are populated with the default value, which in the case of Objects is null. You must create new Entry's and populate your array with them before you start trying to perform operations on them.

    One thing I would strongly suggest is taking the Entry class out of the Phonebook class. It will make declaring an Entry easier to do (mostly syntactically), and allow you to work with the entries without having to deal with creating a phonebook class and operating on that class to access Entry.

Similar Threads

  1. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 09:14 AM
  2. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM