title: Android using float[] as vertices causes OpenGL error 0x502
body: <p>I am trying to use an array of floats for my verticies in OpenGLES 2.0 on Android instead of a FloatBuffer but When I do I glDrawArrays gives the error 0x502 or GL_INVALID_OPERATION.</p>
  
  <p>I do not get this error and everything works fine when I use FloatBuffers. </p>
  
  <p>I have read that this error is normally caused by a program not being set. I only use one shader program and I set it when everything initialized.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public class LineEngine {
      private static final float[] IDENTIY = new float[16];
      private float[] mLinePoints;
      private float[] mLineColors;
      private int mCount;
  
      public LineEngine(int maxLines) {
          Matrix.setIdentityM(IDENTIY, 0);
  
          mLinePoints = new float[maxLines * 2 * 4];
          mLineColors = new float[maxLines * 2 * 4];
  
          reset();
      }
  
      public void addLine(float[] position, float[] color) {
          int offset = mCount * 2 * 4;
          System.arraycopy(position, 0, mLinePoints, offset, 8);
          System.arraycopy(color, 0, mLineColors, offset, 4);
          System.arraycopy(color, 0, mLineColors, offset + 4, 4);
  
          mCount++;
      }
  
      public void reset() {
          mCount = 0;
      }
  
      public void draw() {
          if (mCount &gt; 0) {
              GraphicsEngine.setMMatrix(IDENTIY);
              GraphicsEngine.setColors(mLineColors);
              GraphicsEngine.setVertices4d(mLinePoints);
              GraphicsEngine.disableTexture();
              GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);
  
              int error = GLES20.glGetError();
              if (error != 0)
                  Log.e("OpenGL", "Draw " + error); // Prints error 1282
  
              GraphicsEngine.disableColors();
              reset();
          }
      }
  }
  </code></pre>
  
  <p>This code works fine with no errors</p>
  
  <pre><code>public class LineEngine {
      private static final float[] IDENTIY = new float[16];
      private FloatBuffer mLinePoints;
      private FloatBuffer mLineColors;
      private int mCount;
  
      public LineEngine(int maxLines) {
          Matrix.setIdentityM(IDENTIY, 0);
  
          ByteBuffer byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
          byteBuf.order(ByteOrder.nativeOrder());
          mLinePoints = byteBuf.asFloatBuffer();
  
          byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
          byteBuf.order(ByteOrder.nativeOrder());
          mLineColors = byteBuf.asFloatBuffer();
  
          reset();
      }
  
      public void addLine(float[] position, float[] color) {
          mLinePoints.put(position, 0, 8);
          mLineColors.put(color, 0, 4);
          mLineColors.put(color, 0, 4);
          mCount++;
      }
  
      public void reset() {
          mLinePoints.position(0);
          mLineColors.position(0);
          mCount = 0;
      }
  
      public void draw() {
          if (mCount &gt; 0) {
              mLinePoints.position(0);
              mLineColors.position(0);
              GraphicsEngine.setMMatrix(IDENTIY);
              GraphicsEngine.setColors(mLineColors);
              GraphicsEngine.setVertices4d(mLinePoints);
              GraphicsEngine.disableTexture();
              GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);
  
              int error = GLES20.glGetError();
              if (error != 0)
                  Log.e("OpenGL", "Draw " + error); // no errors
  
              GraphicsEngine.disableColors();
              reset();
          }
      }
  }
  </code></pre>
  
  <p>The related GraphicsEngine code is here</p>
  
  <pre><code>public static void setVertices4d(FloatBuffer vertices) {
      GLES20.glVertexAttribPointer(aVertexHandle, 4, GLES20.GL_FLOAT, false,
              16, vertices);
      GLES20.glEnableVertexAttribArray(aVertexHandle);
      mState.shape = -1;
  }
  
  public static void setVertices4d(float[] vertices) {
      GLES20.glVertexAttrib4fv(aVertexHandle, vertices, 0);
      GLES20.glEnableVertexAttribArray(aVertexHandle);
      mState.shape = -1;
  }
  
  public static void setColors(FloatBuffer colors){
      GLES20.glVertexAttribPointer(aColor, 4, GLES20.GL_FLOAT, false,
              16, colors);
      GLES20.glEnableVertexAttribArray(aColor);
      GLES20.glUniform1i(GraphicsEngine.uEnableColors, 1);
  }
  
  public static void setColors(float[] colors){
      GLES20.glVertexAttrib4fv(aColor, colors, 0);
      GLES20.glEnableVertexAttribArray(aColor);
      GLES20.glUniform1i(GraphicsEngine.uEnableColors, 1);
  }
  </code></pre>
  
  <p>I don't want to use FloatBuffers because they are slower to change than an array of floats.</p>