akhi halder
akhi halder

Reputation: 1

There is no error in the code. But after install, my app crash and rectifying logcat "error is attemp to write to null array"

Here is my code Plane.kt

 init {
    plane[0] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_1)
    plane[1] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_2)
    plane[2] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_3)
    plane[3] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_4)
    plane[4] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_5)
    plane[5] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_6)
    plane[6] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_7)
    plane[7] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_8)
    plane[8] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_9)
    plane[9] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_10)
    plane[10] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_11)
    plane[11] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_12)
    plane[12] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_13)
    plane[13] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_14)
    plane[14] = BitmapFactory.decodeResource(context.resources, R.drawable.plane_15)
    random = Random()
    resetPosition()
}

Here is my code Plane2.kt

open class Plane2(context: Context) : Plane(context) {
override var  plane = arrayOfNulls<Bitmap>(10)
override val bitmap: Bitmap?
    get() = plane[planeFrame]

override val width: Int
    get() = plane[0]!!.width

override val height: Int
    get() = plane[0]!!.height

override fun resetPosition() {
    planeX = -(200 + random.nextInt(1500))
    planeY = random.nextInt(400)
    velocity = 5 + random.nextInt(21)
}

Here is my code GameView.kt

 init {
    tank = BitmapFactory.decodeResource(resources, R.drawable.tank)
    val display = (getContext() as Activity).windowManager.defaultDisplay
    val size = Point()
    display.getSize(size)
    dWidth = size.x
    dHeight = size.y
    rect = Rect(0, 0, dWidth, dHeight)
    planes = ArrayList()
    planes2 = ArrayList()
    missiles = ArrayList()
    explosions = ArrayList()
    for (i in 0..1) {
  open class Plane2(context: Context) : Plane(context) {
override var  plane = arrayOfNulls<Bitmap>(10)
override val bitmap: Bitmap?
    get() = plane[planeFrame]

override val width: Int
    get() = plane[0]!!.width

override val height: Int
    get() = plane[0]!!.height

override fun resetPosition() {
    planeX = -(200 + random.nextInt(1500))
    planeY = random.nextInt(400)
    velocity = 5 + random.nextInt(21)
}
  val plane = Plane(context)
        planes.add(plane)
        val plane2 = Plane2(context)
        planes2.add(plane2)
    }

StartGame.kt

class StartGame : Activity() {
var gameView: GameView? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    this.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    //gameView = GameView(this)
    gameView = GameView(this)
    setContentView(gameView)
}

}

Here is my Logcat error

2019-12-31 01:58:49.006 6311-6311/com.example.shooterplane E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shooterplane, PID: 6311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shooterplane/com.example.shooterplane.StartGame}: java.lang.NullPointerException: Attempt to write to null array
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6810)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 Caused by: java.lang.NullPointerException: Attempt to write to null array
    at com.example.shooterplane.Plane.<init>(Plane.kt:32)
    at com.example.shooterplane.Plane2.<init>(Plane2.kt:7)
    at com.example.shooterplane.GameView.<init>(GameView.kt:183)
    at com.example.shooterplane.StartGame.onCreate(StartGame.kt:16)
    at android.app.Activity.performCreate(Activity.java:7224)
    at android.app.Activity.performCreate(Activity.java:7213)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:201) 
    at android.app.ActivityThread.main(ActivityThread.java:6810) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

What is the logic of this problem?

Upvotes: 0

Views: 49

Answers (1)

Mureinik
Mureinik

Reputation: 311338

Looks like you didn't initialize the plane array in Plane.kt:

init {
    plane = arrayOf(
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_1),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_2),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_3),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_4),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_5),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_6),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_7),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_8),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_9),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_10),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_11),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_12),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_13),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_14),
        BitmapFactory.decodeResource(context.resources, R.drawable.plane_15)
    )
    random = Random()
    resetPosition()
}

Upvotes: 1

Related Questions