28

I am having a problem with the following lines where car is a String array which has not been initialized/has no elements.

String car [];
System.out.println(car.length);

What is a possible solution?

xav
  • 5,452
  • 7
  • 48
  • 57
aman_novice
  • 1,027
  • 3
  • 13
  • 31

8 Answers8

52

Since car has not been initialized, it has no length, its value is null. However, the compiler won't even allow you to compile that code as is, throwing the following error: variable car might not have been initialized.

You need to initialize it first, and then you can use .length:

String car[] = new String[] { "BMW", "Bentley" };
System.out.println(car.length);

If you need to initialize an empty array, you can use the following:

String car[] = new String[] { }; // or simply String car[] = { };
System.out.println(car.length);

If you need to initialize it with a specific size, in order to fill certain positions, you can use the following:

String car[] = new String[3]; // initialize a String[] with length 3
System.out.println(car.length); // 3
car[0] = "BMW";
System.out.println(car.length); // 3

However, I'd recommend that you use a List instead, if you intend to add elements to it:

List<String> cars = new ArrayList<String>();
System.out.println(cars.size()); // 0
cars.add("BMW");
System.out.println(cars.size()); // 1
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 2
    +1 for suggesting lists, which are almost always a more suitable *interface* to work to than arrays. – Andrzej Doyle Feb 01 '11 at 12:57
  • 3
    Your first statement "Since car has not been initialized, it has no length, its value is null" is wrong. Since car has not been initialized, the code won't even compile. Referencing a potentially uninitialized variable is a compile time error. – jarnbjo Feb 01 '11 at 13:33
  • You are right, `javac` indeed throws that error. I've edited my answer to account for that. – João Silva Feb 01 '11 at 14:35
5

Well, in this case the car variable will be null, so dereferencing it (as you do when you access car.length) will throw a NullPointerException.

In fact, you can't access that variable at all until some value has definitely been assigned to it - otherwise the compiler will complain that "variable car might not have been initialized".

What is it you're trying to do here (it's not clear to me exactly what "solution" you're looking for)?

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • well this comes as a part of the dynamic web project, where am not sure whether any value will be stored in the array or not!! And then i want to check whether my array has anything in it or not... – aman_novice Feb 01 '11 at 19:08
3
String car [];

is a reference to an array of String-s. You can't see a length because there is no array there!

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
3

This won't work. You first have to initialize the array. So far, you only have a String[] reference, pointing to null.

When you try to read the length member, what you actually do is null.length, which results in a NullPointerException.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
2

Since you haven't initialized car yet so it has no existence in JVM(Java Virtual Machine) so you have to initialize it first.

For instance :

car = new String{"Porsche","Lamborghini"};

Now your code will run fine.

INPUT:

String car [];
car = new String{"Porsche","Lamborghini"};
System.out.println(car.length);

OUTPUT:

2

Devaarth
  • 241
  • 2
  • 7
2

As all the above answers have suggested it will throw a NullPointerException.

Please initialise it with some value(s) and then you can use the length property correctly. For example:

String[] str = { "plastic", "paper", "use", "throw" };
System.out.println("Length is:::" + str.length);

The array 'str' is now defined, and so it's length also has a defined value.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
1

I think you are looking for this

String[] car = new String[10];
int size = car.length;
Wazy
  • 8,822
  • 10
  • 53
  • 98
0

In Java, we declare a String of arrays (eg. car) as

String []car;
String car[];

We create the array using new operator and by specifying its type:-

String []car=new String[];
String car[]=new String[];

This assigns a reference, to an array of Strings, to car. You can also create the array by initializing it:-

String []car={"Sedan","SUV","Hatchback","Convertible"};

Since you haven't initialized an array and you're trying to access it, a NullPointerException is thrown.

Reethi Joseph
  • 71
  • 2
  • 6