Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2009, 12:24 PM
Authorized User
 
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert bytes to byte[] array

Hello Everyone!
I am having problems trying to pass data through streams.
The Output streams I do not think are letting me pass data that is not of primitive type. I was trying to pass a byte[] and it was coming up null everytime on the other end. My idea is to pass in a loop a bunch of bytes and then put them into a bytearray on the other side. To me it makes sense. I have had some issues trying to perform this conversion.

It is possible to convert several bytes into a byte[] array, correct?

Thanks a lot,
d-mart
 
Old April 23rd, 2009, 08:23 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Of course you can, and the problem must be something else.

Which stream class did you use and which method of that class did you call?
 
Old April 24th, 2009, 08:39 AM
Authorized User
 
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert bytes to byte[] array

Thanks for your reply,

I have a few classes that I am working with. Basically troubleshooting the first problem that I come across I am faced with a Java.lang.NullPointerException.

static byte pDatabyte[];
static char charTest = 'a';
static byte test2;

for (int index = 0; index < dataLength; index++) {

test2= (byte) charTest;
//s.setTestByte(test2) I will use once I get everything else working
pDatabyte[index] = test2;

}

This is my first problem that I came across. The compiler hits a NullPointerException
error and points to my line 54 which is the only error line it hits
pDatabyte[index] = test2; line

For simplicity I just show those first few lines.
Thanks a lot!

D-mart
 
Old April 24th, 2009, 09:21 AM
Authorized User
 
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert bytes to byte[] array

After a few minutes working on it I see what I did wrong. I have to initialize the byte array first instead of just declaring it. Simple problem but I thank you for your reply as well.

static byte[] pDatabyte = new pDatabyte[100];


Thanks
 
Old April 24th, 2009, 10:09 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Glad that you resolved this. Yes, you have to initialize it first, one big source of null pointer is this.





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert byte[] to hex value gunjan.sh C# 2005 4 January 24th, 2008 05:15 AM
convert 4 bytes to bit array Vinny_68 Pro Visual Basic 2005 1 March 21st, 2007 05:40 PM
convert byte[] to hex wkm1925 C# 1 December 14th, 2006 09:07 PM
convert bytes[] into base64binary in asp.net bharat_agarwal XSLT 1 June 20th, 2006 02:04 AM
convert byte to bitmap jlzmut C# 2 December 18th, 2005 05:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.