hadoop-common-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthew Foley <ma...@yahoo-inc.com>
Subject Re: Writable Class with an Array
Date Thu, 17 Mar 2011 21:56:39 GMT
Hi Maha,

There are three things missing from your simplified code needed to give a definitive answer,
so I'll infer them as follows:
-  the specification of "twoIntegers"
	"twoIntegers" seems to be a tuple of ints with get/set accessors, so it is an object (array
of primitives?) not a primitive, right?
-  in the exception "twoIntegersArrayWritable.readFields(twoIntegersArrayWritable.java:46)",
what line 46 refers to
	I'm betting it refers to the line "this.vector[i].setFirstInteger(in.readInt());"
-  the implementation of CreateNewVector(size)
	It seems likely it just does a "new twoIntegers[size];"

If the above is correct, then the problem is that standard Java behavior is to fill the new
array with nulls rather than with empty twoInteger objects.  So in readFields(), when you
enter the loop to read values for vector, the code "this.vector[0]" fetches a null object,
and the continuation "this.vector[0].setFirstInteger()" causes a NullPointerException.

You can fix this in CreateNewVector(), by explicitly allocating a new twoInteger object for
each location in the "vector" array, or in the readFields() loop, whichever suits your program
semantics better.


On Mar 17, 2011, at 2:04 PM, maha wrote:


 I'm stuck with this for two days now ...I found a previous post discussing this, but not
with arrays.

I know how to write Writable class with primitive type elements but this time I'm using an
ARRAY of primitive type element, here it is in a simplified version for easy readability :

public class twoIntegersArrayWritable implements Writable {

	private int vectorSize;
	private twoIntegers[] vector ;

	/* CONSTRUCTORS */  ===================================================

	twoIntegersArrayWritable () {}

	twoIntegersArrayWritable (int size, twoIntegers[] vec) {
		this.vectorSize = size;		
		this.vector = vec;

	/* WRITE FUNCTION */  ===================================================	

	public void write(DataOutput out) {	

		for(int i=0;i<this.vectorSize;i++){
			out.writeInt( vector[i]. getFirstIntger());
			out.writeInt( vector[i]. getSecondIneger());

	/* READ FUNCTION */  ===================================================	

	public void readFields(DataInput in) throws IOException {
		this.vectorSize = in.readInt();		
		this.CreateNewVector (this.vectorSize);
		for(int i=0;i< this.vectorSize;i++){ 				

The errors I get:
	at twoIntegersArrayWritable.readFields(twoIntegersArrayWritable.java:46)
	at org.apache.hadoop.io.SequenceFile$Reader.getCurrentValue(SequenceFile.java:1751)
	at org.apache.hadoop.io.SequenceFile$Reader.next(SequenceFile.java:1879)
	at SequenceFileReader_Integers.main(SequenceFileReader_Integers.java:49)

Or it would work fine but array size is always 0. So it won't read the integer fields.

It would be helpful if someone already has an example of using array in their Writable class.
I appreciate any thought. 


View raw message