2

I have been trying to run a SAVE process in a Java Swing app with the use of Hibernate. But i am getting the following exception every time.

java.lang.NumberFormatException: For input string: "Male"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at frames.user_info_1.saveUserDetails(user_info_1.java:258)
at frames.user_info_1.jButton1ActionPerformed(user_info_1.java:191)
at frames.user_info_1.access$100(user_info_1.java:17)
at frames.user_info_1$2.actionPerformed(user_info_1.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

Here's the code which i used (a user registration).

public void saveUserDetails() {
    try {
        Transaction t = sess.beginTransaction();

        String fn = firstname.getText();
        String ln = lastname.getText();
        String nicno = nic.getText();
        String contact = contactno.getText();

        String gen = (String) jComboBox1.getSelectedItem();
        int gen1 = Integer.parseInt(gen);

        String un = username.getText();
        String pw = new String(password.getPassword());

        User u = new User();
        u.setFirstname(fn);
        u.setLastname(ln);
        u.setNic(nicno);
        u.setPhone(contact);

        Gender g = (Gender) sess.load(Gender.class, gen1);
        u.setGender(g);

        Login login = new Login();
        login.setUser(u);
        login.setUsername(un);
        login.setPassword(pw);

        String utype = "User";
        int utype1 = Integer.parseInt(utype);

        UserType utp = (UserType) sess.load(UserType.class, utype1);
        u.setUserType(utp);

        sess.save(u);
        sess.save(login);
        t.commit();

        System.out.println("User Successfully Saved!");
    } catch (NumberFormatException e) {
        System.out.println("Number Format Exception");
        e.printStackTrace();
    }

jComboBox1 is used to select the User's Gender and it is loaded with the values in the Gender table using a Criteria Search.

I guess the issue is with jComboBox. A help here would be really appreciated. Thanks in advance.

3 Answers 3

6

If your problem is about persisting an enum with Hibernate you can use the @Enumerated annotation to use enum object in your pojo, but persisting it as ordinal

Sign up to request clarification or add additional context in comments.

1 Comment

Adding @Enumerated(EnumType.STRING) worked for me. Thanks!
3

You are trying to parse a String value "Male" to an Integer. It is certainly not a valid number and thus the NumberFormatException.

int gen1 = Integer.parseInt(gen); // This is the problem

You could directly use the String in the load method.

Gender g = (Gender) sess.load(Gender.class, gen); // Use gen instead of gen1

Though even that would not solve your problem, unless gen is the primary key(the identifier for your Gender class). You need to give the id(primary key) as the second parameter to the load() method.

Comments

0

You should check the value entered by the user, suppose it is male (i.e the user input), the code should be like this. The answer given by the user in jcombobox is jcom.

if(jcom.equals("Male"))
          gen=1;
else
          gen=0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.