How to remove this issue about NumberFormatException?

B

bluestar

Hi, all:

I am rookie for coding java and I have one question about queue
class

I use one queue class: ArrayBlockingQueue to save/get my data

I read some data from HW and save into one ArrayBlockingQueue, and
then get from this ArrayBlockingQueue when needing. But it has one
error message: <java.lang.NumberFormatException: Invalid int:
"ffffff94"> when doing poll function

My simple code is below

private ArrayBlockingQueue<Byte> iReadQueueArray = new
ArrayBlockingQueue<Byte>(READBUF_SIZE, true);

byte[] rbuf = new byte[256];

iReadCnt = readfromHW(rbuf, rbuf.length); <---read data from
HW
ret = iReadQueueArray.offer( Byte.valueOf( (rbuf&0xFF) ));

//-----------------------------------------------------------------------//
public int read(byte[] buf) {
Byte mdata;
...
mdata = (Byte)iReadQueueArray.poll(); <---occur error when
polling some data
if( mdata!=null ) {
buf = (byte) (mdata.byteValue()&0xFF);
}
...
return 0;
}
//-----------------------------------------------------------------------//

How to modify offer data into the queue and poll data from the
queue?

Thank you for your help!

BR,
Alan
 
B

bluestar

I change queue to ArrayBlockingQueue<Integer> iReadQueueArray = new
ArrayBlockingQueue<Integer>(READBUF_SIZE, true);
But still it has the same issue

ret = iReadQueueArray.offer(new Integer((int) (rbuf&0xFF)));

//-----------------------------------------------------------------------//
public int read(byte[] buf) {
Integer mdata;
...
mdata = (Integer)iReadQueueArray.poll();
if( mdata!=null ) {
buf = (byte) (mdata.intValue()&0xFF);<---occur error when
casting some data
}
...
return 0;
}
//-----------------------------------------------------------------------//
 
R

Roedy Green

mdata.intValue()&0xFF);<---occur error when

You want to see what mdata is. use .getClass()

from there drill down to find the data.

As far as I know, NumberFormatExceptions only happen when you convert
from String to binary.
I can't see why or where you would be doing that.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 
A

Arved Sandstrom

You want to see what mdata is. use .getClass()

from there drill down to find the data.

As far as I know, NumberFormatExceptions only happen when you convert
from String to binary.
I can't see why or where you would be doing that.
Number objects are more binary than String objects? Who knew?

An NFE is simply if the String format expected by a conversion to a
numeric type is not correct.

As for where it's happening, it's in the error message. I'd want to see
more of the program myself.

AHS
 
R

Roedy Green

Number objects are more binary than String objects? Who knew?

"123" is a string of characters. 3 x 16-bit unicodes. int i=123 is
stored internally as 32-bit binary at run-time as is new Integer( 123
) in a protective object wrapper.

Perhaps it is my age, but it is a long standing tradition to refer
call the process as "converting character/a/alpha/ascii to binary".

Number is a general term that includes byte, int, float, double,
packed decimal, fixed length char numerics. (Think COBOL PL/I). In
Java, Number is an abstract class with ten implementations.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 
L

Lew

Roedy said:
Arved Sandstrom wrote, quoted or indirectly quoted someone who said :

"123" is a string of characters. 3 x 16-bit unicodes. int i=123 is
stored internally as 32-bit binary at run-time as is new Integer( 123
) in a protective object wrapper.

And every real programmer knows that both are binary representations at their heart.

That's all he's saying.
Perhaps it is my age, but it is a long standing tradition to refer
call the process as "converting character/a/alpha/ascii to binary".

A loose description. Arved's was precise.
Number is a general term that includes byte, int, float, double,
packed decimal, fixed length char numerics. (Think COBOL PL/I). In
Java, Number is an abstract class with ten implementations.

Ten implementations that you know of. There might be more.
 
R

Roedy Green

A loose description. Arved's was precise

He was just playing lawyerly putdown games.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top