title: How to write and read the byte array to DataInput and DataOutput Stream
body: <p>Hbase acts as a source and sink for my Map reduce job. I have written my custom writable class called (vector writable) and it has two fields in it. </p>
  
  <pre><code>private DoubleVector vector; // It is a Double Array
  private byte[] rowKey;       // The row key of the Hbase 
  </code></pre>
  
  <p>My mapper emits this as its value, hence I have implemented write and read methods in my vectorWritable class</p>
  
  <pre><code>  @Override
     public final void write(DataOutput out) throws IOException {
     writeVectorCluster(this.vector, this.rowKey, out);       
     }
  
     @Override
     public final void readFields(DataInput in) throws IOException {
     this.vector = readVector(in);
     this.rowKey = readRowKey(in);
     } 
  
  public static void writeVectorCluster(DoubleVector vector, byte[] rowkey, DataOutput out)
              throws IOException {
          out.writeInt(vector.getLength());
              for (int i = 0; i &lt; vector.getDimension(); i++) {
                  out.writeDouble(vector.get(i));
              }
              int length = rowkey.length;
              out.writeInt(length);
  
             //Is this the right way ?
              out.write(rowkey);
          }
  
  
  public static DoubleVector readVector(DataInput in) throws IOException {
          int length = in.readInt();
          DoubleVector vector = null;
              vector = new DenseDoubleVector(length);
              for (int i = 0; i &lt; length; i++) {
                  vector.set(i, in.readDouble());
              }
          return vector;
      }
  
     @SuppressWarnings("null")
      public static byte[] readRowKey(DataInput in) throws IOException {
          int length = in.readInt();
          byte [] test = null;
              for (int i = 0; i &lt; length; i++) {
                  // getting null pointer exception here
                  test[i] = in.readByte();
              }
          return test;
      }
  </code></pre>
  
  <p>I get a NullPointerException when I try to read the rowKey from the input stream. The readVector method works fine though and I am getting the right value.</p>
  
  <p>How can I write the byte array in the DataInput Stream so that I could retrive it in my Output streams </p>
  
  <p><em><strong>UPDATE : SOLVED</em></strong>
  This is the update of my rowKey method which works fine. Thanks @Perception</p>
  
  <pre><code>public static byte[] readRowKey(DataInput in) throws IOException {  
          int length = in.readInt();
          byte[] theBytes = new byte[length];
          in.readFully(theBytes); 
          return theBytes;
      }
  </code></pre>