0

Hello everyone i am trying to do a exercise from a Java book and what i need to do is draw lines and within a textfield i have to say how far the distance between the lines have to be. All of this has to be in a loop so no hardcoded lines.

i have made everything like i thought it should be done but i get some weird error in my console that is keep coming. the error code is as followed:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499) at h03.LinePanel.paintComponent(LinePanel.java:30) at javax.swing.JComponent.paint(JComponent.java:1037) at javax.swing.JComponent._paintImmediately(JComponent.java:5106) at javax.swing.JComponent.paintImmediately(JComponent.java:4890) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:812) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694) at javax.swing.RepaintManager.access$700(RepaintManager.java:41) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:702) at java.awt.EventQueue.access$400(EventQueue.java:82) at java.awt.EventQueue$2.run(EventQueue.java:663) at java.awt.EventQueue$2.run(EventQueue.java:661) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:672) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

since i am new to Java i don't know what i did wrong and how to troubleshoot this error so i would appreciate any help! the code in my panel that i use for this is

DrawLines lines = new DrawLines();

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    int positionY = getHeight() - Integer.parseInt(afstand.getText()); // absolut positioning

    int yPos = 0;

    while(yPos <= positionY) {

        lines.drawLines(g, 0, yPos, getWidth(), yPos);
        yPos = yPos + Integer.parseInt(afstand.getText());
    }

}

public void actionPerformed(ActionEvent e) {

    try {

        repaint();


    }
    catch(NumberFormatException err) {

        JOptionPane.showMessageDialog(null, "something went wrong! heeft u wel    een waarde opgegeven?");

    }

}

2 Answers 2

2

The issue is exactly what it says: you call Integer.parseInt() on an empty String, in your example: afstand.getText(), and it does not like it.

java.lang.NumberFormatException: For input string: "" at
[...]
java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499)

To avoid the exception you could catch the exception:

try {
    int input = Integer.parseInt(afstand.getText());
    //rest of your code
} catch (NumberFormatException e) {
    //let the use know that the entry is invalid
}
Sign up to request clarification or add additional context in comments.

12 Comments

I'd like to add the suggestion, that you check the input for non-Number chars before trying to parse it to Integer.
But how come it already checks the textfield when i start debugging? i mean it's not in the constructor so it has to wait till i have put in a value or am i thinking the wrong way?
@Fildor I've done that - although that check should probably done in the component itself, for example through a Document, rather than in the paintComponent method.
@Reshad paintComponent is called as soon as your component is visible - and it means you have not had time to enter anything yet (unless you populate the field before it is shown).
@assylias I agree. This is a special case I guess.
|
0

getText().toString() to get the value of your Textfield. You can try System.out.println(afstand.getText) and with toString to see the difference.

Otherwise there is no integer value in your Textfield.

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.