• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing null values within a multi-dimensional array.  RSS feed

 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).

I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).

I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?

Assuming you mean 'replace' rather than 'remove', if arr is a reference to your array
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok it seemed to work but I am confused why trying to access arr[i][j] inside the if statement wouldn't give you a NullPointerException. Because after all you're trying to access a null value? Doesn't this defeat the purpose of NPE?

What am I not understanding?

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a difference between a null pointer and a null value. if the pointer is pointing at something that no longer exists, that is null pointer. pointing at something that does exist, but has a null value is different.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:there is a difference between a null pointer and a null value. if the pointer is pointing at something that no longer exists, that is null pointer. pointing at something that does exist, but has a null value is different.

I think of it like this...If i said "go to the 2nd shelf, third slot over, get the box, and tell me what's in it.".

there are three things that could happen:

The box is there and has something in it - you say "the box contains <whatever>"
The box is there and is empty - you say "The box is empty"
There isn't a box there - you say "There is no box, so I can't see what's in it".

The first is what you would normally expect.
The second is a null value. you found the box, but it is empty.
The third is a null pointer - i 'pointed' you to something that wasn't there.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:Ok it seemed to work but I am confused why trying to access arr[i][j] inside the if statement wouldn't give you a NullPointerException. Because after all you're trying to access a null value? Doesn't this defeat the purpose of NPE?

What am I not understanding?


Consider this code:



Would you expect a NullPointerException from this code?
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Bob Ivanovich wrote:Ok it seemed to work but I am confused why trying to access arr[i][j] inside the if statement wouldn't give you a NullPointerException. Because after all you're trying to access a null value? Doesn't this defeat the purpose of NPE?

What am I not understanding?


Consider this code:



Would you expect a NullPointerException from this code?


A NullPointerException might occur because you might be working with 'output' which is null (or wasn't initialized.) Correct?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!