• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the length of a null array object?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following and it gives me null pointer exception. I just want to verify if what is the actual value of a length of an array?

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be no length, because there is no array.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:I tried the following and it gives me null pointer exception.

What else would you expect?

If o is an object (and arrays in Java are objects) and is null, then 'o.anything' will always throw a NullPointerException.

Winston
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If o is an object (and arrays in Java are objects) and is null, then 'o.anything' will always throw a NullPointerException.

Not true! If 'anything' is a static method the program will compile and work
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thumbs up for being pedantic
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Not true! If 'anything' is a static method the program will compile and work

Absolutely right. Now, whether it should or not is a different matter...

Totally forgot about that case because I never do it.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:I just want to verify if what is the actual value of a length of an array?

If someone said "tell me how many pages are in the book on my nightstand" and when you looked you couldn't find a book...how many pages would you say it has?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:If someone said "tell me how many pages are in the book on my nightstand" and when you looked you couldn't find a book...how many pages would you say it has?

I'd say that it is a book without a cover and the number of pages is 0
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to start saying "null pointer exception" whenever someone can't find something so I can see how many new friends I make.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have CS buddies who, when I ask them if they want coffee or something else, respond with "yes".

We've never become very close
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I have CS buddies who, when I ask them if they want coffee or something else, respond with "yes".

We've never become very close


Hahaha, I have a habit of doing that
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Can you tell me what time is it?
- Yes.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wife, pointing at top shelf in cupboard, "Can you reach that?"
Me: "Yep."
Wife: "<not allowed to print that here>"

Seriously, though -- this question caught me by surprise. It shouldn't have; I'm certain I've seen it posted over the years on other boards. As Winston put it, "What else would you expect." Reminds me of when I was a kid working for my dad at his shop. I'd ask him for a raise and his reply was always, "Sure. Give yourself 10%." My pay was zero -- it was after all a family business.

Back to the question though -- why would you (or anyone) want to know the length of nothing?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:Back to the question though -- why would you (or anyone) want to know the length of nothing?

Wow! That's so deep... Let's post this question on http://forums.philosophyforums.com
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Thumbs up for being pedantic

I suppose if we are going to be pedantic...there is no such thing as a "null array object". What the OP has is a null reference to an object of type Array.

I think. I know it's a null reference.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I suppose if we are going to be pedantic...there is no such thing as a "null array object". What the OP has is a null reference to an object of type Array.

If we are goint to be pedantic... The OP has a null reference to an object of type Object[]
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I have CS buddies who, when I ask them if they want coffee or something else, respond with "yes".



3 logicians walk into a bar. The bartender asks them "Do all 3 of you want a beer"

"Don't know"
"Don't know"
"Yes!!"
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:
Stephan van Hulst wrote:I have CS buddies who, when I ask them if they want coffee or something else, respond with "yes".



3 logicians walk into a bar. The bartender asks them "Do all 3 of you want a beer"

"Don't know"
"Don't know"
"Yes!!"

A logician's wife is having a baby. The doctor immediately hands the newborn to the dad. The wife says, "Is it a boy or a girl?"
The logician says, "Yes."
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:3 logicians walk into a bar. The bartender asks them "Do all 3 of you want a beer"

<pedantic>
Shouldn't that have been: "Do all 3 of you want a drink?"
</pedantic>

Winston
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jayesh A Lalwani wrote:3 logicians walk into a bar. The bartender asks them "Do all 3 of you want a beer"

<pedantic>
Shouldn't that have been: "Do all 3 of you want a drink?"
</pedantic>

Winston


No, because then the first logician would have answered "Yes".
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No, because then the first logician would have answered "Yes".

Assuming, of course, it's only possible reason for going into a bar (personally, I can't think of another).

Winston
 
Amir Al Take
Greenhorn
Posts: 15
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:I tried the following and it gives me null pointer exception. I just want to verify if what is the actual value of a length of an array?



Hi Winston,

Null means empty or nothing. When you assign a null as value to String variable or any object it means they that they don't hold currently anything. Thus garbage collector in Java will collect them. So the reason why you get NullPointerException is because you try to check the length of "nothing".
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a 'null' array, your array has absolutely nothing in it. You 'would' be shown a NullPointerException. There is 'nothing' to point to.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the example used way above. If you ask how many pages are in the book on my bookshelf and you go to see. You find NO book whatsoever. That would be a NullPointerException. There is 'no' book to point to. Absolutely nothing in the book because there is no book.
You create an empty array, you are doing the exact same thing. The program tries to find the array and finds it BUT there is nothing in it to answer how long is the array because it is simply null. This would leave you NULL, causing the NullPointerException.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy McNally wrote:When you have a 'null' array, your array has absolutely nothing in it.

Jeremy McNally wrote:You create an empty array, you are doing the exact same thing. The program tries to find the array and finds it BUT there is nothing in ...

No. There is no array at all. That is different from having an array that has nothing in it. There is no array; not even an empty array.

You can have an array with zero elements:

That is not the same as having no array at all:

Note that variables in Java (of non-primitive types) are references to objects. The variable is just a pointer to an object; it's not the object itself. Having a variable does not mean you have an object. If the variable is null, it points to no object.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree, good point. There was no array object created. It was only a reference variable to an array that COULD be created. The array was assigned a null value so exactly, there is no array at all what, so ever. Thank you for correcting me.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
You can have an array with zero elements:



Its a bit surprising java allows you to do this. I think its just one for the philosophy forums, but if a container of some type does not have the capacity to hold something EVER, can it be considered a container? My vote is no.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:
I tried the following and it gives me null pointer exception.


The problem here is that obj holds no object. Obj is null so you get a null pointer exception.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Its a bit surprising java allows you to do this. I think its just one for the philosophy forums...

No it isn't; not by a long chalk - and for exactly the reasons we've been discussing.

A 0-length array IS an array, and IS an object; null isn't. Indeed, if you have a method that returns an array, you should almost always return a 0-length array in preference to null.

Winston
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
A 0-length array IS an array, and IS an object; null isn't.


I understand that's the case, the point is whether that case models real world containers accurately or not.

Is the following statement true or false:

Something that doesn't have the capacity to contain is not a container.

Again my vote is that its true, but if your vote is that its false that isn't a java programming language disagreement its a philosophical one.

Back to java though, when you create an array of size -5 you get a Negative Array Size exception. It doesn't seem too unreasonable that java have something like a ZeroArraySizeException, especially since there is a memory cost associated with creating an array that you absolutely can't do anything with. Or you could just make creating negative fixed length arrays legal.

I'm not trying to say that's what they should have done, just could have without violating any real world principles.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My philosophical viewpoint -- and principle as well -- is that zero is a valid number for this purpose. If you have a method which returns all the files in a directory as an array of files -- and we do -- then since it's perfectly legitimate for a directory to contain no files, it should be legitimate to return an array with zero entries to reflect that situation. Throwing an exception would be just wrong because empty directories aren't abnormal things which require special handling.

Edit: And with a nod of the head to the original post, returning null instead of a zero-length array wouldn't be much better. That would still carry the implication that an empty directory was somehow different from a non-empty directory in a significant way.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that directories have the capacity to contain files. Saying "this directory contains no files" is different than saying "this directory does not have the capacity to contain files". Of course we can say that a directory that does not have the capacity to contain files does not contain any file. But we can also say that a directory that does not have the capacity to contain files (any files whatsoever) is not a directory. So which comes first?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which comes first? Clearly the directory comes first, and then the array which is supposed to represent it comes afterwards. Since a directory can contain zero or more files (not one or more files), then allowing the array which represents it to contain zero or more entries is the natural thing to do.

Edit: If you're trying to say that the array is not a perfect metaphor for a directory, then I agree with you. For example the array imposes an ordering on the files in the directory, which is unnecessary.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the array is created before its returned. I can see reasoning for a creating a book cover if you might decide to add pages. But if you know when you create it that you will NEVER add pages, the creation of the book cover is a useless exercise. So the empty book cover is created and returned merely as a means of saying there are no pages. It seems like the java creators might have decided there's a better way to relay the information that there are no pages than by creating unnecessary covers. Or not! Just saying its one way to go.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's only if you think the fact of zero files is a special case which needs to be highlighted. I don't think it is, and if the user does need to find that out then it isn't hard to do. Which is my point, that philosophical and principle business: Forcing the user to treat zero as a special case is often wrong.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, that's only if you think the fact of zero files is a special case which needs to be highlighted. I don't think it is, and if the user does need to find that out then it isn't hard to do. Which is my point, that philosophical and principle business: Forcing the user to treat zero as a special case is often wrong.


Well when you go through the process of creating a useless array to merely indicate that a directory has zero files it seems like you're the one treating zero like a special case. You can always just not return anything.

But whatever, I don't want to fall into the trap of arguing that having zero lengthed fixed arrays being illegal would be something superior for java. I'm usually all for flexibility, and I'm mostly playing devil's advocate here. I just think that from certain perspectives its not so unreasonable. I mean it seems like OP thought that whenever you create an array there has to be some sort of length associated it, so he was wondering what that default length was.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Well when you go through the process of creating a useless array to merely indicate that a directory has zero files it seems like you're the one treating zero like a special case. You can always just not return anything.


So we have a method that returns files in directory.
So its definition might be like this:

You said You can always just not return anything.
Well, you can't. You have to return something. Even if it is null.

So, suppose you want to write code that prints files from given directory.
You write:

It's nice and elegant. Even if there are no files in directory, the method returns an empty array. The for loop does nothing and we are happy.

And this is how the code would look like if the method returned null in case there are no files:
We added some code just to handle special case of returning null. This could have been avoided.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:I just think that from certain perspectives its not so unreasonable...

Possibly, but it really isn't worth getting too "philosophical" about.

As Pawel pointed out, there are clear practical reasons for having a method that is declared to return an array always return an array, even if, in order to do so, that array may have a length of 0.
By the same token, you could argue that "" isn't a String, but would you want to return null instead of it?

NPEs can be some of the toughest of Exceptions to unravel because, more often than not, they are an effect, not a cause; so if you have a simple solution to avoid them, my advice is to use it.

Winston
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
You said You can always just not return anything.
Well, you can't. You have to return something. Even if it is null.


null is a keyword used to represent nothing (not anything). If I said "you don't have to use the return keyword" then I would be wrong. Yes, returning empty fixed arrays means you don't have to null check as much. That's fine, and is a legit reason for allowing empty fixed arrays, but it doesn't solve the philosophical problem that was brought up.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Tyson Lindner wrote:I just think that from certain perspectives its not so unreasonable...

Possibly, but it really isn't worth getting too "philosophical" about.


Probably not, that doesn't mean its not a philosophical problem at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!