-3

I am doing a school management system project, everything is good except when I try to click the save button it returns the JOption error message that phone must be integer although it is already. I must say I have a similar form for teacher registration and that one works. How can it be?

private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {                                            
try{    
        int day = Integer.valueOf((String)jComboBoxDay.getSelectedItem());
        int month = Integer.valueOf((String)jComboBoxMonth.getSelectedItem());
        int year = Integer.valueOf((String)jComboBoxYear.getSelectedItem());
        String birthDate = ""+day+month+year;
        String firstName = jTextFieldFirstName.getText();            
        String lastName = jTextFieldLastName.getText();
        String address = jTextFieldAddress.getText();
        String email = jTextFieldEmail.getText();
        int phoneNumber = Integer.parseInt((jTextFieldPhoneNumber).getText());
        String gender = (String)jComboBoxGender.getSelectedItem(); 
        String religion = jTextFieldReligion.getText();
        String contactTeacher =jTextFieldContactTeacher.getText();
        int contactPhoneNumber = Integer.parseInt((jTextFieldContactPhoneNumber).getText());
        int momID = Integer.parseInt((jTextFieldMotherID).getText());
        int fatherID = Integer.parseInt((jTextFieldFatherID).getText());

        Reset();
            Students student = new Students(birthDate,firstName,lastName,address, email,phoneNumber,gender,religion,contactTeacher,contactPhoneNumber,momID,fatherID);
            studentsControl.createStudents(student);
            loadTable();
    } 
            catch (NumberFormatException exception)
    {
        JOptionPane.showMessageDialog(null,"Phone must be an integer ","Error",JOptionPane.ERROR_MESSAGE);
        jTextFieldPhoneNumber.setText("");
}
}             
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Well we can't actually tell where the error is because it could be a format exception from any of those fields. – markspace Apr 27 '17 at 17:24
  • Try printing the stack trace and look up if it's really the phone number. Your error message is hardcoded without any connection to reality... – Martin B. Apr 27 '17 at 17:27
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Always copy/paste error and exception output! 4) To expand on the last comment, I mean the output of changing `catch (NumberFormatException exception) {` **to** `catch (NumberFormatException exception) { exception.printStackTrace();` – Andrew Thompson Apr 27 '17 at 17:32
  • i tried the stack trace and i got the error, if i understand it right i should just paste it? what i don't understand is why an exactly similar form works for teacher registration and the phone number is an attribute inherited by both teacher and student classes. – Marius EraZnus Apr 27 '17 at 21:21
  • What happens if `day`, `month` or `year` fail to parse? What amount `monID` or `fatherID `? It's impossible for use to debug your code without more details – MadProgrammer Apr 27 '17 at 21:46
  • I have tried also with int birthDate = Integer.parseInt(jTextFieldBirthDate.getText()); because maybe there was an error when selecting the item from the combobox. Needless to say that i have redone the studentsMapper,studentsController,studentsTableModel and even the registration form 3 times now... always getting that ridiculous error, but than again i have 2 other forms for parents and teachers which work, built on identical algorithms – Marius EraZnus Apr 29 '17 at 14:31

1 Answers1

0

You're getting the month description from jComboBoxMonth object.

Try getting the index instead by calling getSelectedItem method and adding 1.

alayor
  • 4,537
  • 6
  • 27
  • 47