Interpreting Run-time Errors

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Back to Chapter Topics


Introduction

This section describes how to read java run-time errors, how to interpret what they mean, and provide some possible leads on what could be wrong with your program.

   

{{{Body}}}

Note: This article is for reference only. You will not be tested on any of the material on this page. It is only provided to you for help in debugging your programs.

What are Run-time Errors?

Run-time errors occur when you are running your java program, as opposed to compile-time errors, which occur when you are compiling your program. Here is a very short guide to some of the more common run-time errors that happen in COMP 1010, as well as some advice on how to solve them. (Compile-time errors are covered in a separate article.)

Errors Using Java

  • NoClassDefFound: see this posting to solve this problem.
  • Exception in thread "main" java.lang.NoSuchMethodError: main This usually is the result of having a typo in your public static void main line.

Input Errors

  • NumberFormatException. This error is typically caused by using Integer.parseInt or Double.parseDouble on a String value which cannot be interpreted properly as an integer or double. For instance, if a String x has the value "3.bad", then Integer.parseInt(x) would cause this error to occur. This can often be caused by the user typing in a wrong value, which ultimately, can't always be accounted for.


Math Errors

  • java.lang.ArithmeticException: / by zero This indicates that you have tried to evaluate an expression x/y where y has the value zero. One possible solution is to use an if statement to test if y is zero before executing the division in question.
  • NaN While this is technically not a run-time error, it is worth mentioning here. If your code tries to do something sketchy (in the mathematical sense of the word, e.g., taking the square root of a negative number), the result is NaN -- "Not a number".


String Run-time Errors

  • java.lang.NullPointerException: In this course, this is typically caused by trying to use a String before it has been given a value, usually by asking for its length or characters from it. (Note: In other COMP courses --- COMP 1020, COMP 2140, etc. --- this problem could come up in a number of other exciting ways.)
  • java.lang.StringIndexOutOfBoundsException: String index out of range: N (where N is an integer value). This is usually caused by trying to use charAt on an index (N) that is greater than the length of the string. Re-examine the expression that is giving the integer N.

Array Run-time Errors

  • java.lang.ArrayIndexOutOfBoundsException: N (where N is an integer value). This is usually caused by trying to use charAt on an index (N) that is greater than the length of the string. Re-examine the expression that is giving the integer N.

Other Run-time Errors

  • java.util.NoSuchElementException: No line found This error can occur if you are using TextPad and are trying to use the Scanner class for input instead of JOptionPane.showInputDialog. If you want to play with Scanner, you must have the "Capture Output" option unchecked under Configure->Preferences->Tools-> Run Java Application.