Using Integer.parseInt crashes my program Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Using Integer.parseInt crashes my program without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have looked all over the internet and cannot find my out why this is happening to my program. Basically I am trying to make a TextField that that makes a string of numbers, and that string is turned into an integer and the label is changed to the number +1 Whenever I attempt to use Integer.parseInt It crashed

Here is my Code

public class dfadsfa {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    dfadsfa window = new dfadsfa();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public dfadsfa() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    String Text;
    int Number = Integer.parseInt(Text); //This line Screws it up

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNumber = new JLabel("");
        lblNumber.setBounds(198, 181, 61, 16);
        frame.getContentPane().add(lblNumber);

        textField = new JTextField();
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Text = textField.getText();
                lblNumber.setText(Number+1+"");
            }
        });

        textField.setBounds(163, 140, 130, 26);
        frame.getContentPane().add(textField);
        textField.setColumns(10);


    }

}

This is the message that shows up-

java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at dfadsfa.<init>(dfadsfa.java:42)
at dfadsfa$1.run(dfadsfa.java:21)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Answer

Text is not initialized , So it gives a NumberFormatException. Here Integer.parseInt() method will give the NumberFormatException if the passed string does not contain a parsable integer.

Use

String Text = "0" to solve the problem

We are here to answer your question about Using Integer.parseInt crashes my program - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji