android - NullPointerException when trying to access inherited field -
i`m getting npe when trying access overridden variable in parent class
java.lang.nullpointerexception: attempt length of null array @ app.deadmc.materiallivewallpaper.model.square.<init>(square.kt:29) @ app.deadmc.materiallivewallpaper.model.cube.<init>(cube.kt:8) @ app.deadmc.materiallivewallpaper.renderer.materialrenderer.onsurfacecreated(materialrenderer.kt:40) @ android.opengl.glsurfaceview$glthread.guardedrun(glsurfaceview.java:1548) @ android.opengl.glsurfaceview$glthread.run(glsurfaceview.java:1286)
i show simplified code undestanding problem
i have class drawing square
open class square(renderer: readyrenderer) { val coords_per_vertex = 3 open val trianglecoords = floatarrayof( //first triangle -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, //second triangle -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f) private val vertexcount:int = trianglecoords.size / coords_per_vertex init { val bb = bytebuffer.allocatedirect( trianglecoords.size * 4) //other code } }
square works correctly, want draw cube, inherit square , add more coordinates
open class cube(renderer: readyrenderer):square(renderer) { val size = 1.0f override val trianglecoords = floatarrayof( //front side // triangle 1 -size, size, size, // top-left -size, -size, size, // bottom-left size, -size, size, // bottom-right // triangle 2 size, -size, size, // bottom-right size, size, size, // top-right -size, size, size, // top-left //and side, left, top, right same way ) }
seems logic, right? variables have values , there should no place npe.
but when try create instance of cube
val cube = cube(this@materialrenderer)
i catch nullpointerexception in square class @ line:
private val vertexcount:int = trianglecoords.size / coords_per_vertex
even if assign value directly
private val vertexcount:int = 2
i npe @ square class inside init block @ line:
val bb = bytebuffer.allocatedirect( trianglecoords.size * 4)
so have 2 questions:
1. how can fix , still use inheritance?
2. supposed work way?
if @ editor, warned using non-final property in constructor. happens because when create cube
, need create square
first. when square
creating, cube
not initialized. (trianglecoords
null) therefore, trianglecoords.size
throw exception.
how can fix , still use inheritance?
you should pass parameter.
open class square(protected val trianglecoords: floatarray = floatarrayof( //first triangle -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, //second triangle -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f)) { val coords_per_vertex = 3 private val vertexcount: int = trianglecoords.size / coords_per_vertex init { val bb = bytebuffer.allocatedirect( trianglecoords.size * 4) //other code } } open class cube(val size: float = 1.0f) : square(floatarrayof( //front side // triangle 1 -size, size, size, // top-left -size, -size, size, // bottom-left size, -size, size, // bottom-right // triangle 2 size, -size, size, // bottom-right size, size, size, // top-right -size, size, size, // top-left //and side, left, top, right same way )) { }
if think ugly, can write factory method , pass array through parameter private constructor.
is supposed work way?
yes
Comments
Post a Comment