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

how do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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