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?

1
  • The solution probably depends on your problem. Care to tell us what it is? Commented Feb 1, 2011 at 13:34

8 Answers 8

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for suggesting lists, which are almost always a more suitable interface to work to than arrays.
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.
You are right, javac indeed throws that error. I've edited my answer to account for that.
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)?

1 Comment

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...
3
String car [];

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

Comments

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.

Comments

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

Comments

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.

Comments

1

I think you are looking for this

String[] car = new String[10];
int size = car.length;

2 Comments

wazzy: wat if the size of array is not known before hand?
Then an array may not be your best choice. Look at a list as @Joao recommended.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.