<< 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

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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