NullPointerException while drawing bitmap in onDraw method in custom View android

Viewed 20

I was making a simple custom DrawingView class which can draw with small brushes. But When I try to run it gets Nullpointer Exception

this is the stacktrace.

    Process: com.mahidev.kidsdrawingapp, PID: 19949
    java.lang.NullPointerException
        at com.mahidev.kidsdrawingapp.DrawingView.onDraw(DrawingView.kt:64)
        at android.view.View.draw(View.java:22635)
        at android.view.View.updateDisplayListIfDirty(View.java:21472)
        at android.view.View.draw(View.java:22335)
        at android.view.ViewGroup.drawChild(ViewGro

this is drawingview - 64th line. I didn't get any clue what is happening?

        canvas.drawBitmap(mCanvasBitmap!!, 0f, 0f, mCanvasPaint)

package com.mahidev.kidsdrawingapp

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.util.Size
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View

class DrawingView(context: Context, attrs: AttributeSet) : View(context, attrs)
{

    private var mDrawPath : CustomPath? = null
    private var mCanvasBitmap: Bitmap? = null
    private var mDrawPaint: Paint? = null
    private var mCanvasPaint: Paint? = null
    private var mBrushSize: Float = 0.toFloat()
    private var color = Color.BLACK

    private var canvas : Canvas? = null
    private val mPaths = ArrayList<CustomPath>()

    init
    {
        Log.i("Mahi", "inside init")
        setUpDrawing()
    }

    private fun setUpDrawing()
    {

        Log.i("Mahi", "inside setUpDrawing")
        mDrawPaint = Paint()
        mDrawPath = CustomPath(color, mBrushSize)
        mDrawPaint!!.color = color
        mDrawPaint!!.style = Paint.Style.STROKE
        mDrawPaint!!.strokeJoin = Paint.Join.ROUND
        mDrawPaint!!.strokeCap = Paint.Cap.ROUND
        mCanvasPaint = Paint(Paint.DITHER_FLAG)
       // mBrushSize = 20.toFloat()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)
    {
        Log.i("Mahi", "inside onSizeChanged")
        super.onSizeChanged(w, h, oldw, oldh)
        mCanvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        canvas = Canvas(mCanvasBitmap!!)
    }

    fun setSizeForBrush(newSize: Float)
    {
        mBrushSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, newSize, resources.displayMetrics)
        mDrawPaint!!.strokeWidth = mBrushSize
    }



    override fun onDraw(canvas: Canvas) {
        Log.i("Mahi", "onDraw")
        super.onDraw(canvas)
        canvas.drawBitmap(mCanvasBitmap!!, 0f, 0f, mCanvasPaint)

        for(path in mPaths)
        {
            mDrawPaint!!.strokeWidth = path!!.brushThickness
            mDrawPaint!!.color = mDrawPath!!.color
            canvas.drawPath(path, mDrawPaint!!)
        }

        if(!mDrawPath!!.isEmpty)
        {
            mDrawPaint!!.strokeWidth = mDrawPath!!.brushThickness
            mDrawPaint!!.color = mDrawPath!!.color
            canvas.drawPath(mDrawPath!!, mDrawPaint!!)
        }

            }

    override fun onTouchEvent(event: MotionEvent?): Boolean
    {
        Log.i("Mahi", "inside onTouchEvent")
        val touchX = event?.x
        val touchY = event?.y

        when(event?.action)
        {
            MotionEvent.ACTION_DOWN ->
            {
                mDrawPath!!.color = color
                mDrawPath!!.brushThickness = mBrushSize

                mDrawPath!!.reset()
                if (touchX != null) {
                    if (touchY != null) {
                        mDrawPath!!.moveTo(touchX, touchY)
                    }
                }
            }

            MotionEvent.ACTION_MOVE ->
            {
                if (touchY != null) {
                    if (touchX != null) {
                        mDrawPath!!.lineTo(touchX, touchY)
                    }
                }
            }

            MotionEvent.ACTION_UP ->
            {
                mPaths.add(mDrawPath!! )
                mDrawPath = CustomPath(color, mBrushSize)
            }

            else -> return false

        }

        invalidate()

        return true

        return super.onTouchEvent(event)
    }



    internal inner class CustomPath(var color: Int, var brushThickness: Float) : Path()
    {

    }
}


0 Answers
Related