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); 
4
  • 4
    docs.oracle.com/javase/tutorial/essential/exceptions/… Commented Apr 24, 2015 at 11:57
  • Could you share your zeit decleration and a sample value of zeit taken from debug mode? Commented Apr 24, 2015 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) Commented Apr 24, 2015 at 12:02
  • all variabels are declared and tested Commented Apr 24, 2015 at 16:45

2 Answers 2

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 } } 
Sign up to request clarification or add additional context in comments.

Comments

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:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.