There's a key difference between a null array and an empty array. This is a test for null.
int arr[] = null;
if (arr == null) {
System.out.println("array is null");
}
"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:
arr = new int[0];
if (arr.length == 0) {
System.out.println("array is empty");
}
An alternative definition of "empty" is if all the elements are null:
Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
if (arr[i] != null) {
empty = false;
break;
}
}
or
Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
if (ob != null) {
empty = false;
break;
}
}
Answer from cletus on Stack OverflowThere's a key difference between a null array and an empty array. This is a test for null.
int arr[] = null;
if (arr == null) {
System.out.println("array is null");
}
"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:
arr = new int[0];
if (arr.length == 0) {
System.out.println("array is empty");
}
An alternative definition of "empty" is if all the elements are null:
Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
if (arr[i] != null) {
empty = false;
break;
}
}
or
Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
if (ob != null) {
empty = false;
break;
}
}
ArrayUtils.isNotEmpty(testArrayName) from the package org.apache.commons.lang3 ensures Array is not null or empty
Videos
A small snippet to show the difference:
Copy// declare a array variable that can hold a reference.
String [] array;
// make it null, to indicate it does not refer anything.
array = null;
// at this point there is just a array var initialized to null but no actual array.
// now allocate an array.
array = new String[3];
// now make the individual array elements null..although they already are null.
for(int i=0;i<array.length;i++)
{
array[i] = null;
}
// at this point we have a array variable, holding reference to an array,
// whose individual elements are null.
No, the first one makes each element of the array null, the length of the array will still be array.length, the second will set the array variable to null.
Technically speaking, there's no such thing as a null array; but since arrays are objects, array types are reference types (that is: array variables just hold references to arrays), and this means that an array variable can be null rather than actually pointing to an array:
int[] notAnArray = null;
An empty array is an array of length zero; it has no elements:
int[] emptyArray = new int[0];
(and can never have elements, because an array's length never changes after it's created).
When you create a non-empty array without specifying values for its elements, they default to zero-like values — 0 for an integer array, null for an array of an object type, etc.; so, this:
int[] arrayOfThreeZeroes = new int[3];
is the same as this:
int[] arrayOfThreeZeroes = { 0, 0, 0 };
(though these values can be re-assigned afterward; the array's length cannot change, but its elements can change).
An array has its members initialized to their default values. For int the default is 0. For an Object it's null. A null array is a null Array Reference (since arrays are reference types in Java).
JLS-4.12.5 Initial Values of Variables says in part
For type int, the default value is zero, that is, 0.
and
For all reference types (§4.3), the default value is null.
I recently learnt that an empty array IN JAVA is filled with "0" not null and I was wondering how you will be able to determine the size of the array if you cant say "i != null " how can you tell how many elements are in the array?
Hey r/learnprogramming, I'm doing this project for school and I can't seem to figure out why I'm receiving this null pointer exception. I believe the object is null on initialization, but I'm trying to use a setter method from inside the class to scan in a line of text one at a time from a file.
Luckily it's not due for about a week (thankful that I started this early) so I'm not in too much of a panic, but I'd like to at least get past this error so I can start writing some of my methods.
The error I receive is
Exception in thread "main" java.lang.NullPointerException at Micsa.main(n00737298.java:21)
Here is my code
I'd appreciate any help or even just a pointer (lol) in the right direction. I feel like it's right in front of me but maybe I've just been staring at my monitor too long.
Thank you!