Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Array copying question

Thread view: 
Raga - 08 Oct 2006 14:56 GMT
Hi,

Say I have a 2 dimensional byte array appendixBlock[10][] & another 1
dimensional byte array, say, tempBytes[].

I do this in the code:

               String tempString = new
String(((String)object).getBytes(), "UTF-16");
               byte[] tempBytes = tempString.getBytes();
               bytesNum = tempBytes.length;
               appendixBlock[appendixIndex] = null;
               for(int x=0; x<bytesNum;x++)
               {
                   appendixBlock[appendixIndex][x] = tempBytes[x];
               }

Am getting NullPointer Exception due to the above. Whereas if I do:

appendixBlock[appendixIndex] = tempBytes;

am not getting any exception. Can anyone please throw some light on
what's wrong?
Jussi Piitulainen - 08 Oct 2006 15:24 GMT
> I do this in the code:
...
>     appendixBlock[appendixIndex] = null;
>     for(int x=0; x<bytesNum;x++)
[quoted text clipped - 3 lines]
>
> Am getting NullPointer Exception due to the above. Whereas if

You shouldn't be too surpised to find that
appendixBlock[appendixIndex] is null just after
you explicitly set it so.

Set it to a new byte[bytesNum] instead.
Arne Vajhøj - 08 Oct 2006 17:13 GMT
>>     appendixBlock[appendixIndex] = null;
>>     for(int x=0; x<bytesNum;x++)
[quoted text clipped - 9 lines]
>
> Set it to a new byte[bytesNum] instead.

Sounds as a plan !

:-)

And after than, then using System.arraycopy could
get rid of the for loop.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.