libgdx/libgdx · error · NullPointerException
NullPointerException
Error message
NullPointerException
What it means
Thrown by FloatBuffer.wrap(float[] array, int start, int len) when array is null — the null check precedes all index validation, per the documented NullPointerException. wrap(float[]) (single-arg) delegates here with 0/array.length, so a null array fails identically. The buffer would wrap the array directly (no copy), hence null is rejected up front.
Source
Thrown at backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/nio/FloatBuffer.java:74
return wrap(array, 0, array.length);
}
/** Creates a new float buffer by wrapping the given float array.
* <p>
* The new buffer's position will be {@code start}, limit will be {@code start + len}, capacity will be the length of the
* array.
* </p>
*
* @param array the float array which the new buffer will be based on.
* @param start the start index, must not be negative and not greater than {@code array.length}.
* @param len the length, must not be negative and not greater than {@code array.length - start}.
* @return the created float buffer.
* @exception IndexOutOfBoundsException if either {@code start} or {@code len} is invalid.
* @exception NullPointerException if {@code array} is null.
* @since Android 1.0 */
public static FloatBuffer wrap (float[] array, int start, int len) {
if (array == null) {
throw new NullPointerException();
}
if (start < 0 || len < 0 || (long)start + (long)len > array.length) {
throw new IndexOutOfBoundsException();
}
FloatBuffer buf = BufferFactory.newFloatBuffer(array);
buf.position = start;
buf.limit = start + len;
return buf;
}
/** Constructs a {@code FloatBuffer} with given capacity.
*
* @param capacity The capacity of the buffer */
FloatBuffer (int capacity) {
super(capacity);
}View on GitHub (pinned to 97f4086187)
Solutions
- Null-check before wrapping: if (arr == null) arr = EMPTY_FLOATS; (a shared zero-length array) or branch on null and skip the buffer entirely.
- Fix the producer to return float[0] instead of null for absent data, making the empty case uniform.
- Where null is meaningful, handle it at the call site with an explicit decision (default buffer, error) rather than letting wrap() throw.
Example fix
// before float[] colors = meshData.getColors(); // null when absent FloatBuffer cb = FloatBuffer.wrap(colors); // NullPointerException // after float[] colors = meshData.getColors(); FloatBuffer cb = (colors == null) ? FloatBuffer.allocate(0) : FloatBuffer.wrap(colors);
Defensive patterns
Strategy: validation
Validate before calling
FloatBuffer safeWrap(float[] arr) {
return arr == null ? FloatBuffer.allocate(0) : FloatBuffer.wrap(arr);
} Prevention
- Make producers return float[0] instead of null for absent data.
- Null-check optional attribute arrays before wrapping.
- Use a shared EMPTY_FLOATS constant for the empty case.
When it happens
Trigger: Calling FloatBuffer.wrap(null) or wrap(nullArray, 0, 0); passing a lazily-initialized field that was never assigned; a data-source method returning null (optional/absent data) piped straight into wrap().
Common situations: Optional mesh attributes (e.g. missing color array) modeled as null in asset loaders; map.get(key) results used without a null check before wrapping; refactoring that changed a supplier's contract from empty-array to null-on-empty.
Related errors
- NullPointerException
- ReadOnlyBufferException
- BufferUnderflowException
- IndexOutOfBoundsException
- UnsupportedOperationException
AI-assisted analysis of libgdx/libgdx@97f4086187 (2026-08-14).
Data as JSON: /api/errors/2b5da43c27752bf6.
Report an issue: GitHub.