<< Chapter < Page Chapter >> Page >

An IndexOutOfBounds exception

As a result, the program throws an IndexOutOfBounds exception. The error produced under JDK 1.3 looks something like the following:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2at java.util.ArrayList.RangeCheck (Unknown Source)at java.util.ArrayList.get (Unknown Source)at Worker.doIt(Ap147.java:27) at Ap147.main(Ap147.java:16)

Attempting to access an element with a negative index value would produce the same result.

An ArrayIndexOutOfBounds exception

A similar result occurs if you attempt to access an element in an ordinary array object outside the bounds of the index values determined by the size ofthe array. However, in that case, the name of the exception is ArrayIndexOutOfBounds .

Back to Question 7

Answer 6

B. Runtime Error

Explanation 6

The infamous NullPointerException

Interestingly, one of the first things that you read when you start reading Java books, is that there are no pointers in Java . It is likely that shortly thereafter when you begin writing, compiling, and executing simple Javaprograms, one of your programs will abort with an error message looking something like that shown below :

Exception in thread "main" java.lang.NullPointerException atWorker.doIt(Ap146.java:23) atAp146.main(Ap146.java:16)

What is a NullPointerException?

Stated simply, a NullPointerException occurs when you attempt to perform some operation on an object using a reference that doesn'trefer to an object.

That is the case in this program

The following code fragment declares a local reference variable and initializes its value to null .

void doIt(){ ArrayList ref = null;

(A reference variable in Java must either refer to a valid object, or specifically refer to no object (null). Unlike a pointer in C and C++, aJava reference variable cannot refer to something arbitrary.)

In this case, null means that the reference variable doesn't refer to a valid object.

No ArrayList object

Note that the code in the above fragment does not instantiate an object of the class ArrayList and assign that object's reference to the reference variable.

(The reference variable doesn't contain a reference to an object instantiated from the class named ArrayList , or an object instantiated from any class for that matter.)

Call a method on the reference

However, the code in the next fragment attempts to add a String object's reference to a nonexistent ArrayList object by calling the add method on the reference containing null.

ref.add("ABC ");

This results in the NullPointerException shown earlier .

What can you do with a null reference?

The only operation that you can perform on a reference variable containing null is to assign an object's reference to the variable. Any other attemptedoperation will result in a NullPointerException .

Back to Question 6

Answer 5

C. ABC DEF GHI

Explanation 5

The purpose of this program is to

  • Continue to illustrate the use of java packages, and
  • Illustrate the use of the Java import directive.

Program contains an import directive

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask