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

    }

}
Reshad
  • 2,570
  • 8
  • 45
  • 86

2 Answers2

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
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I'd like to add the suggestion, that you check the input for non-Number chars before trying to parse it to Integer. – Fildor Sep 23 '12 at 17:27
  • 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? – Reshad Sep 23 '12 at 17:33
  • @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. – assylias Sep 23 '12 at 17:33
  • @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 Sep 23 '12 at 17:35
  • @assylias I agree. This is a special case I guess. – Fildor Sep 23 '12 at 17:37
  • @assylias hmm and where do i have to put this try and catch block? and what do you mean with populate the field? – Reshad Sep 23 '12 at 17:51
  • @Reshad Quick and dirty: put the try/catch block in your paintComponent method. – assylias Sep 23 '12 at 17:56
  • @assylias i see in the console the error shown but after i put in a value it works kinda but this is a very dirty way is there not a better way to do this? – Reshad Sep 23 '12 at 18:03
  • @Reshad I think you should review your design and parse that information outside the paintComponent method. See for example: http://stackoverflow.com/questions/3412135/how-to-validate-if-text-entered-is-a-numeric-number Or you can use an InputVerifier: http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification – assylias Sep 23 '12 at 18:06
  • But how can i draw the lines in a while loop outside the paintComponent? – Reshad Sep 23 '12 at 18:20
  • @Reshad Read the input as in the examples I linked to, store the result in a field and access that field from paintComponent. – assylias Sep 23 '12 at 18:21
  • i changed it a bit and i get a different error now.. > Exception in thread "main" java.lang.NullPointerException at h03.LinePanel.(LinePanel.java:26) at h03.Frame.(Frame.java:16) at h03.Main.main(Main.java:10) the link where u can see my code https://github.com/reshadf/Java/blob/master/DeBasis/src/h03/LinePanel.java – Reshad Sep 23 '12 at 18:48
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.

JackTools.Net
  • 734
  • 6
  • 13