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?");

    }

}
4

2 回答 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
}
于 2012-09-23T17:20:36.517 回答
0

getText().toString() 以获取您的文本字段的值。您可以尝试 System.out.println(afstand.getText) 并使用 toString 来查看差异。

否则,您的文本字段中没有整数值。

于 2012-09-23T17:25:21.000 回答