reading and writing to a file using ArrayList()

Started by
13 comments, last by markg 14 years, 2 months ago
As remigius said, this class does not have override the toString() method from Object. That is why you don't get meaningful results when you try to print the contact to the file.
//Given a Contact c;System.out.println(c);

translates to
//Given a Contact c;System.out.println(c.toString());

If you don't override the method, then it will use Object's toString method, which is what you are currently outputting.

The method you would have to write in Contact is toString()
String toString()


Otherwise you could print each part of Contact when you're writing it to the file:
PrintWriter writeFile = new PrintWriter(file);for(Contact c : aSaveContacts){    writeFile.println(c.getName());    writeFile.println(c.getAddress());    //Continue here...}
Advertisement
Yep,
Your right, I did put in a toString() and it works. I am able to write to a file and when I open the file up I see all the info in english.
The other problem that I am now having is getting it to read in that file. here's the code. for reading in a file.

/* code begin.

public static void retrieveFile() throws FileNotFoundException, ParseException {
// TODO Auto-generated method stub

ArrayList<Contact> list = new ArrayList<Contact>();
list.clear();
Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file you would like to work with: ");

String fileName = input.nextLine();


Scanner read = new Scanner( new File(fileName));

while(read.hasNext()){

String line = read.next();
String[] lineData = line.split("|");
String date = lineData[0];
String name = lineData[1];
String phone = lineData[2];
String city = lineData[4];
String state = lineData[5];
String zip = lineData[6];

Contact contact = new Contact();
contact.setdateOfBirth(date);
contact.setName(name);
contact.setPhoneNumber(phone);
Address newAddress = new Address();
contact.setAddress(newAddress);
contact.setCity(city);
contact.setState(state);
contact.setZip(zip);

list.add(contact);
}

read.close();
}

*/ end code


Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException
at edu.itttech.labs.week06.Contact.toString(Contact.java:113)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at edu.itttech.labs.week06.AddressBookDemo2.retrieveFile(AddressBookDemo2.java:110)
at edu.itttech.labs.week06.AddressBookDemo2.main(AddressBookDemo2.java:52)
Quote:Original post by markg
Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException
at edu.itttech.labs.week06.Contact.toString(Contact.java:113)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at edu.itttech.labs.week06.AddressBookDemo2.retrieveFile(AddressBookDemo2.java:110)
at edu.itttech.labs.week06.AddressBookDemo2.main(AddressBookDemo2.java:52)

That's actually indicative of just one error :). Your toString method in the Contact class is trying to call a method on a null reference. This is occuring on line 113 of Contact.java - inspect that line to see what's up.
Quote:Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException


Cmon Mark, we're trying to help but put some effort into it. A NullPointerException is probably the singlemost frequent error you'll see from Java in your early programming days, so you should familiarize yourself with it. Also figuring out what a stack trace is should be one of your top priorities, these are invaluable to debugging.

As for the error itself I'll refer to mattd's reply above, but I'll wager your Address object in your Contact is probably null. The code you posted also doesn't match the error message, there is no System.out.println(Contact) anywhere in retrieveFile yet the error says there was. You're making it a challenge to help you [smile]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
I know, I Know, I am going to have to put a little more effort into it. Now that you metioned about that stack trace I do rememger the instructor explaining it to us in class. I can be very forgetfull sometimes. It's all this learning that I am not used to. Any way I think I have to put this project on the back burner for now because I have been given another project that I have to start on for school. So I want to thank you for all the help that you have given me. I plan on picking up where I left off on this address book project probably at the end of this semester and I am sure I'll get it to work.

This topic is closed to new replies.

Advertisement