• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying array issue

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below snippet of code throws a nullPointerException but if I replace with the following snippet of code it "works" but the array points to the array passed in and that's no good because the array coming in needs to change on each call. How can I effectively copy the array into the instance array lineCoords?



 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the documentation for arraycopy, you'll see that you can't pass a null value in for the array. You've got to create the array first (at the size you want). If you think about it, that has to be the case as you're passing the reference by value. If you pass null in, it can't possibly result in lineCoords pointing at a new object.

However, I suggest you check Arrays.copyOf instead, as that will create the array for you.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are sure about the size of the array you want to pass, you could just make aan array of that size, make lineCoords, point to it, and then copy the elements of coords to it. Although its is a bit cumbersome, I beleive their must be easier methods...
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to do the copying yourself when methods already exist to do so, Matthew's suggestion is a good idea.

Hunter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic