<< Chapter < Page Chapter >> Page >
This module explains the various exceptions that are raised by the Java Runtime Environment.

Out of range

These exceptions are the most common and are caused by attempting to access an array or string with an index that is outside the limits of the array orstring. C programmers will be familiar with the difficult bugs that are caused by such errors which “smear” memory belonging to other variables; in Javathe errors cause an exception immediately when they occur thus facilitating debugging.


This exception is thrown when attempting the index an array a[i] where the index i is not within the values 0 and a.length-1 , inclusive. Zero-based arrays can be confusing; since the length of the array is larger than the last index, it is not unusual to write the following in a for -statement by mistake:

final static int SIZE = 10; int a = new int[SIZE];  for (int i = 0; i <= SIZE; i++)     // Error, <= should be <  for (int i = 0; i <= a.length; i++) // Better, but still an error


This exception is thrown by many methods of the class String if an index is not within the values 0 and s.length() , inclusive. Note that s.length() is valid and means the position just after the last character in the string.


A variable in Java is either of a primitive type such as int or boolean or of a reference type : an array type, the type of a class defined in the Java API such as String , or the type of a class that you define such as MyClass . When declaring a variable of a reference type, you only get a variable that can hold a reference to an object of thatclass. At this point, attempting to access a field or method of the class will cause the exception NullPointerException to be thrown: The name of the exception is an anachronism, because there are no (explicit) pointers in Java.

class YourClass {   static MyClass my;    // Can hold a reference but doesn't yet    public static void main(String[] args) {     my.m(5);            // Throws NullPointerException  } }

Only after instantiating the class and executing the constructor do you get an object:

class YourClass {   static MyClass my;    // Can hold a reference but doesn't yet    public static void main(String[] args) {     my = new MyClass(); // Instantiates an object                        //   and assigns the reference     my.m(5);            // Throws NullPointerException  } }

This exception is not often seen in this context because Java style prefers that the declaration of the variable include the instantiation as itsinitial value:

class YourClass {   // Declaration + instantiation of class variable  static MyClass my1 = new MyClass();    public static void main(String[] args) {    my1.m(5);            // OK     // Declaration + instantiation of local variable    MyClass my2 = new MyClass();     my2.m(5);            // OK  } }

Sometimes, you cannot initialize a variable at its declaration; an obvious place to initialize an instance variable is within its constructor:

class YourClass {   MyClass[] my;          // Can't initialize here because ...    YourClass(int size) {  // ... size of array different for each     object my = new MyClass[size];   }}

The exception is likely to occur when you declare an array whose elements are of reference type. It is easy to forget that the array containsonly references and that each element must be separately initialized:

MyClass[] my = new MyClass[4];       // Array of references my[1].m(5);                          // Raises an exception  for (int i = 0; i < my.length; i++)   my[i] = new MyClass();             // Instantiate objects my[1].m(5);                          // OK

Finally, NullPointerException will occur if you get a reference as a return value from a method and don't know or don't check that the value isnon-null: You could know this if the method has been verified for a postcondition that specifies a non-null return value.

Node node = getNextNode(); if (node.key > this.key) ...         // Error if node is null!  if (node != null) {                  // This should be done first   if (node.key > this.key) ... }

Computational errors

The following three exceptions occur when you try to convert a string to a number and the form of the string does not match that of a number, for example "12a3" .


This exception is thrown by the class Scanner , which is a class introduced into version 5 of Java to simplify character-based input to programs.


This exception is thrown by the method format in class String that enables output using format specifiers as in the C language.


This exception is thrown by methods declared in the numeric “wrapper” classes such as Integer and Double , in particular by the methods parseInt and parseDouble that convert from strings to the primitive numeric types.


This exception is thrown if you attempt to divide by zero.

Important note
Most computational errors do not result in the raising of an exception! Instead, the result is an artificialvalue called NaN , short for Not a Number . This value can even be printed:

double x = Math.sqrt(-1);  // Does not throw an exception! System.out.println(x);

Any attempt to perform further computation with NaN does not change the value. That is, if x is NaN then so is y after executing y = x+5 . It follows that an output value of NaN gives no information as to the statement that first produced it. You will have to set breakpoints or use printstatements to search for it.

Insufficient memory

Modern computers have very large memories so you are unlikely to encounter these exceptions in routine programming. Nevertheless, they can occur as a side effectof other mistakes.


This exception can be thrown if you run out of memory:

int a = new int[100000000];


A stack is used to store the activation record of each method that is called; it contains the parameters and the local variables as well as other informationsuch as the return address. Unbounded recursion can cause the Java Virtual Machine to run out of space in the stack:

int factorial(int n) {   if (n == 0) return 1;  else return n * factorial(n + 1);  // Error, you meant n - 1 }

Questions & Answers

where we get a research paper on Nano chemistry....?
Maira Reply
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
Nasa has use it in the 60's, copper as water purification in the moon travel.
nanocopper obvius
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Compile and runtime errors in java. OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10913/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Compile and runtime errors in java' conversation and receive update notifications?