Search code examples
androidclasskotlinmaze

How to fix Null can not be a value of a non-null type in android maze game with Kotlin


I was following a this tutorial to create a maze in android but im getting an error when i return null in the getNeighbour method

error: Null can not be a value of a non-null type Cell

Any help on what i am doing wrong its appreciated.

I tried with Cell? but couldn't make it work, it gives me more errors

Here is my code:

class GameView: View{
//revisar por que hace cosas raras con los limites de cols y rows
private val COLS:Int = 7
private val ROWS: Int = 12
private var cellSize: Float = 0F
private var hMargin: Float = 0F
private var vMargin: Float = 0F
private val wallPaint = Paint()
private val random:Random = Random()

constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
    wallPaint.color = Color.WHITE
    wallPaint.strokeWidth = 1F
    createMaze()

}
fun getNeighbour(cell:Cell): Cell {
    var vecinos: ArrayList<Cell> = ArrayList()

    //vecino izquierda
    if(cell.col > 0) {
        if (!cells[cell.col - 1][cell.row].visited) {
            vecinos.add(cells[cell.col - 1][cell.row])
        }
    }
    //vecino derecha
    if(cell.col < COLS - 1) {
        if (!cells[cell.col + 1][cell.row].visited) {
            vecinos.add(cells[cell.col + 1][cell.row])
        }
    }
    //vecino arriba
    if(cell.row > 0) {
        if (!cells[cell.col][cell.row - 1].visited) {
            vecinos.add(cells[cell.col ][cell.row - 1])
        }
    }
    //vecino abajo
    if(cell.row < ROWS - 1) {
        if (!cells[cell.col][cell.row + 1].visited) {
            vecinos.add(cells[cell.col][cell.row + 1])
        }
    }
    if (vecinos.size > 0) {
        var index = random.nextInt(vecinos.size)
        return vecinos[index]
    }else {

 // i get this error: Null can not be a value of a non-null type 
 // Cell
        return null
    }
}

fun removeWall(current:Cell,next:Cell){
    if (current.col == next.col && current.row == next.row +1){
        current.topWall = false
        next.bottomWall = false
    }
    if (current.col == next.col && current.row == next.row -1){
        current.bottomWall = false
        next.topWall = false
    }
    if (current.col == next.col + 1 && current.row == next.row){
        current.leftWall = false
        next.rightWall = false
    }
    if (current.col == next.col - 1 && current.row == next.row +1){
        current.rightWall = false
        next.leftWall = false
    }
}

var cells: Array<Array<Cell>>  = Array(COLS, {Array(ROWS, {Cell()})})

fun createMaze(){
    var stack = Stack<Cell>()
    var current:Cell
    var next:Cell

    for(x in 0 until COLS){
        for(y in 0 until ROWS){
            cells[x][y] = Cell(x,y)
        }
    }
    current = cells[0][0]
    current.visited = true

    do{
        next = getNeighbour(current)
        removeWall(current,next)
        stack.push(current)
        current = next
        current.visited = true

    }while (!stack.empty())

}
override fun onDraw(canvas: Canvas) {
    canvas.drawColor(Color.DKGRAY)
    val ancho = width
    val alto = height
    println(ancho)
    println(alto)
    if(ancho/alto < COLS/ROWS){
        cellSize = ancho/(COLS+1).toFloat()
    }else{
        cellSize = alto/(ROWS+1).toFloat()

    }
    hMargin = (ancho - COLS*cellSize)/2
    vMargin = ((alto - ROWS*cellSize)/2)

    canvas.translate(hMargin,vMargin)

    for(x in 0 until COLS){
        for(y in 0 until ROWS){
            if (cells[x][y].topWall){
            canvas.drawLine(
                x*cellSize,
                y*cellSize,
                (x+1)*cellSize,
                y*cellSize,
                wallPaint)
            }

            if (cells[x][y].leftWall){
                canvas.drawLine(
                    x*cellSize,
                    y*cellSize,
                    x*cellSize,
                    (y+1)*cellSize,
                    wallPaint)
            }

            if (cells[x][y].bottomWall){
                canvas.drawLine(
                    x*cellSize,
                    (y+1)*cellSize,
                    (x+1)*cellSize,
                    (y+1)*cellSize,
                    wallPaint)
            }

            if (cells[x][y].rightWall){
                canvas.drawLine(
                    (x+1)*cellSize,
                    y*cellSize,
                    (x+1)*cellSize,
                    (y+1)*cellSize,
                    wallPaint)
            }
        }
    }

}

}

class Cell(var col:Int = 0, var row: Int = 0 ){
var topWall = true
var leftWall = true
var bottomWall = true
var rightWall = true
var visited = false

}

Solution

  • from the syntax point of view

    // first change
    fun getNeighbour(cell:Cell): Cell -> fun getNeighbour(cell:Cell): Cell?
    
    // second change
    var next:Cell -> var next:Cell? = null
    
    // third change
    next = getNeighbour(current)
    if(next != null) {
      // you process code goes here
    }
    

    But from a design point of view, fun getNeighbour(cell:Cell): Cell means there is always a neighbor can be found.

    If it is not the case, your should change it to Cell? and the following code should takes care of the null case.