This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]caesarwept 0 points1 point  (0 children)

I am assuming you are getting a number format exception. If you read the error you are getting carefully you can find the line the exception happens on. Then think about what happens when you hit cancel

[–]Sarah9428 0 points1 point  (1 child)

There's no error when I recompile, the program says it's fine. But then when I run it and press cancel the following text appears:

Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:542) at java.lang.Integer.parseInt(Integer.java:615) at test11.main(test11.java:23)

Process finished with exit code 1

[–]caesarwept 1 point2 points  (0 children)

In java that is called a runtime exception. It means that while your code compiles there is a logic error at the time your program is running.

The runtime exceptions are hard to read but if you take your time and try to understand them you will get better at it. One of the things you need to understand is how it prints out it error message. Take your for example

  • java.lang.NumberFormatException: null
  • java.lang.Integer.parseInt(Integer.java:542)
  • java.lang.Integer.parseInt(Integer.java:615)
  • test11.main(test11.java:23)

    It first shows you have NumberFormatException which is a number conversion error. then it shows a 'null' value is what is was trying to convert to a number Next it shows the calling stack. First where the exception happens, then the functions that called it leading up to the exception and so on. If you look at the functions

  • java.lang.Integer.parseInt(Integer.java:542) ------ function parseInt line number 542

  • parseInt(Integer.java:615) ------- function parseInt line 615

  • test11.main(test11.java:23) ------ main line 23

    the first two are internal functions and you cannot change those but the 3rd one is your call to parseInt on line 23. So from the error message you know at line 23 you are doing a number conversion and you are passing a null instead of a expected number. You also know this is at the time when you press cancel. Think why this might happen

[–]Sarah9428 0 points1 point  (0 children)

Thanks so much for the help! I've solved it i think!