why cannot update?

Hi, i am doin a program that is suppose to update my customerData.txt. i getText() user input, then call addCustomer() which is in the CustomerDBVer2 class. My problem is that, it throws the

JOptionPane.showMessageDialog(null, error.getMessage(),"Failed to update", ERROR_MESSAGE)

my customerData.txt looks like:

john;lim;jol;male;12;2;1965;123456;ang mo kio;abc;abc

can please tell me what is wrong?

thx

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

publicclass CustNewMenu2extends JPanelimplements ActionListener

{

//some swing code

publicvoid actionPerformed (ActionEvent e)

{

if (e.getSource() == saveButton){

String firstName = firstNameTF.getText();

String lastName = lastNameTF.getText();

String userID = userIDTF.getText();

String gender = (String)genderCB.getSelectedItem();

String birthDate = birthDateTF.getText();

String birthMonth = (String) birthMonthCB.getSelectedItem();

String birthYear = birthYearTF.getText();

String phone = phoneTF.getText();

String address = addressTF.getText();

String password = passwordPF.getText();

String confirmPass = confirmPassPF.getText();

JOptionPane.showMessageDialog(null,"in e.getSource == save","save",

JOptionPane.ERROR_MESSAGE);

try{

CustomerDBVer2 custDB =new CustomerDBVer2();

custDB.addCustomer(firstName, lastName, userID, gender, birthDate, birthMonth, birthYear, phone,

address, password, confirmPass);

}

catch (Exception error){

JOptionPane.showMessageDialog(null, error.getMessage(),"Failed to update",

JOptionPane.ERROR_MESSAGE);

return;

}

}}

}

publicvoid addCustomer(String firstName,String lastName, String userID, String gender,String birthDate, String birthMonth,

String birthYear, String telephone, String address, String password, String confirmPass)

throws Exception

{

String file ="customerData.txt";

customerList.add(new CustomerVer2(firstName, lastName, userID, gender, birthDate,

birthMonth, birthYear, telephone, address, password, confirmPass));

try{

BufferedWriter writer =new BufferedWriter (new FileWriter(file));

PrintWriter outFile =new PrintWriter(writer);

for (int i = 0; i < customerList.size(); i++){

CustomerVer2 f = (CustomerVer2) customerList.get(i);

outFile.println(f);

System.out.println(f);

}

outFile.close();

}

catch (FileNotFoundException e){

Exception error =new Exception(file +" not found");

throw error;

}

catch (IOException e){

Exception error =new Exception("Error reading " + file);

throw error;

}

}

[4617 byte] By [sas0rizaa] at [2007-10-2 11:23:41]
# 1
What's the error message you get.Post the Stacktrace of the Exception you get.
andiha at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 2
Give a full information about the exception thrown.What exception is thrown? From where?
hiwaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 3

when i want to update, the JOptionPane below pop up.

i put the try and catch in the actionPerformed(), which is inside CustomerNewMenu2 extends JPanel

thx

try {

CustomerDBVer2 custDB = new CustomerDBVer2();

custDB.addCustomer(firstName, lastName, userID, gender, birthDate, birthMonth, birthYear, phone,

address, password, confirmPass);

}

catch (Exception error) {

JOptionPane.showMessageDialog(null, error.getMessage(),"Failed to update",

JOptionPane.ERROR_MESSAGE);

return;

}

sas0rizaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 4

You've allready told us that the error popup is shown.

Without the error message / stack trace nobody will be able to help you

Print the StackTrace

catch (Exception error) {

error.printStackTrace();

JOptionPane.showMessageDialog(null,error.getMessage(),"Failed to update",

JOptionPane.ERROR_MESSAGE);

return;

}

and post it here so we can have a look at it.

andiha at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 5
> error.getMessage()What does it print?Also, doerror.printStackTrace();in your catch clause.
hiwaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 6

Hi, thx bcoz of the error.printStackTrace() i know what my problem is. it cannot update bcoz it has java.lang.nullPointerException. but after it is updated it overwrite my previous data and doesnt save in the format that i want.

john;lim;jol;male;12;2;1987;123564;Yi chu kang;abc;abc

but it shows the following

First Name: wertw Last Name: wertw Gender: Male Birthday: 2-March 1968 Telephone: 132445 Address: sgar

thx

sas0rizaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi, thx bcoz of the error.printStackTrace() i know what my problem is. it cannot update bcoz it has java.lang.nullPointerException. but after it is updated it overwrite my previous data and doesnt save in the format that i want.

john;lim;jol;male;12;2;1987;123564;Yi chu kang;abc;abc

but it shows the following

First Name: wertw Last Name: wertw Gender: Male Birthday: 2-March 1968 Telephone: 132445 Address: sgar

thx

sas0rizaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 8
The output depends on the toString method of CustomerVer2
andiha at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 9
> The output depends on the toString method of> CustomerVer2thanks a lot!! but it still overwrites my previous data.....
sas0rizaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 10
> but it still overwrites my previous data.....That's what your code says.
andiha at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 11
You're creating a FileWriter instance that is set to overwrite the existing file. There is a FileWriter constructor that takes a boolean parameter for append.(Not that it matters but this OP is proof that the forum code tags don'g always help - guh!)
Laszlo.a at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...
# 12
THANKSSS. to andih and laszlo.... it is working nowbb ;p
sas0rizaa at 2007-7-13 4:27:58 > top of Java-index,Java Essentials,Java Programming...