0

I wanted to make a Java program with Numberfields and a button. When you click the button it waits and the time that was put in the Numberfield.The Integers work fine, but the function does not work.

Here is the code:

public void jButton1_ActionPerformed(ActionEvent evt) {
    sekunden = Integer.parseInt(jNumberField3.getText()); 
    minuten = Integer.parseInt(jNumberField2.getText());
    stunden = Integer.parseInt(jNumberField1.getText());
    zeit = sekunden + minuten*60 + stunden*60*60;//berechnet die zeit in sekunden
    TimeUnit.SECONDS.sleep(zeit);//here is the error
    System.out.println("zeit");
} // end of jButton1_ActionPerformed

The error message looks like this:

ES.java:78:27: error: unreported exception InterruptedException; must be caught or declared to be thrown TimeUnit.SECONDS.sleep(zeit);
Manjunath N
  • 1,365
  • 11
  • 22
Gilgamesch
  • 313
  • 2
  • 10
  • 24
  • 4
    https://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html – Radiodef Apr 24 '15 at 11:57
  • Could you share your zeit decleration and a sample value of zeit taken from debug mode? – Semih Eker Apr 24 '15 at 11:59
  • You need to use a try-catch on the sleep function. Sleeping throws an InterruptedException in the event that Thread.interrupt is called during the sleep. This information is available when you google the "unreported exception InterruptedException; must be caught or declared to be thrown" ;; In addition, you could simply make the "jButton1_ActionPerformed" function throw the error but then you need to process it [the error] at one step back (the generated function that calls this function) – Wayne Apr 24 '15 at 12:02
  • all variabels are declared and tested – Gilgamesch Apr 24 '15 at 16:45

2 Answers2

1

Method call TimeUnit.SECONDS.sleep(zeit) throws an InterruptedException which has to be caught:

public void jButton1_ActionPerformed(ActionEvent evt) {
   try {
    sekunden = Integer.parseInt(jNumberField3.getText()); 
    minuten = Integer.parseInt(jNumberField2.getText());
    stunden = Integer.parseInt(jNumberField1.getText());
    zeit = sekunden + minuten*60 + stunden*60*60;//berechnet die zeit in sekunden
    TimeUnit.SECONDS.sleep(zeit);//here is the error
    System.out.println("zeit");
    }catch (InterruptedException e){
      //handle the exception
   }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Jens
  • 67,715
  • 15
  • 98
  • 113
1

You need to handle a potential InterruptedException that may happen when you call sleep(). This is why your code is getting a compilation error.

Please take a look at Catching and Handling Exceptions on JavaDocs to more details on exception handling.

In your case, you could easily solve it by handling the exception such as:

public void jButton1_ActionPerformed(ActionEvent evt) {
    sekunden = Integer.parseInt(jNumberField3.getText()); 
    minuten = Integer.parseInt(jNumberField2.getText());
    stunden = Integer.parseInt(jNumberField1.getText());
    zeit = sekunden + minuten*60 + stunden*60*60;//berechnet die zeit in sekunden
    try {
        // tries to call sleep
        TimeUnit.SECONDS.sleep(zeit);//here is the error
    } catch (InterruptedException e) {
        // handles any possible exception during the call to "sleep()"
        e.printStackTrace(); // prints the exception stack trace to the console
    }
    System.out.println("zeit");
} // end of jButton1_ActionPerformed

Some good references in exception handling are:

Community
  • 1
  • 1
Bruno Toffolo
  • 1,504
  • 19
  • 24