简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:“Male”

[英]java.lang.NumberFormatException: For input string: “Male”

I have been trying to run a SAVE process in a Java Swing app with the use of Hibernate. 我一直在尝试使用Hibernate在Java Swing应用程序中运行SAVE过程。 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. jComboBox1用于选择用户的性别,并使用条件搜索在Gender表中加载值。

I guess the issue is with jComboBox. 我想问题是jComboBox。 A help here would be really appreciated. 这里的帮助将非常感激。 Thanks in advance. 提前致谢。

You are trying to parse a String value "Male" to an Integer. 您正在尝试将字符串值“Male”解析为Integer。 It is certainly not a valid number and thus the NumberFormatException . 它当然不是有效数字,因此也不是NumberFormatException

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

You could directly use the String in the load method. 您可以直接在load方法中使用String。

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). 虽然即使这样也无法解决您的问题,但除非gen是主键(您的Gender类的标识符)。 You need to give the id (primary key) as the second parameter to the load() method . 您需要将id (主键)作为load() method的第二个参数。

如果您的问题是关于使用Hibernate持久化枚举,您可以使用@Enumerated注释在您的pojo中使用枚举对象,但将其作为序数持久化

You should check the value entered by the user, suppose it is male (ie the user input), the code should be like this. 您应该检查用户输入的值,假设它是男性(即用户输入),代码应该是这样的。 The answer given by the user in jcombobox is jcom. 用户在jcombobox中给出的答案是jcom。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM