An Exception happened in customizing Controlfx SpreadsheetView type, cell, editor

24 Views Asked by At

I got a NPE in customizing Cell, Editor & CellType components in Controlfx's. I hope someone helps me, thank you.

I list my customizing follow:

  1. I extended a class CellType to create Cells and Editors which is called by SpreadsheetView

  2. Cells is extended from SpreadsheetCellBase

  3. Editor is an editor with textfield

StringCell

    inner class StringCell(
        row: Int,
        col: Int,
        rowSpan: Int,
        colSpan: Int,
        type: SpreadsheetCellType<String>
    ): SpreadsheetCellBase(
        row, col, rowSpan, colSpan, type
    )

Editor

inner class StringCellEditor(
        view: SpreadsheetView,
        private val columnName: String,
        private val map: MutableMap<String, Any>
        ): SpreadsheetCellEditor(view) {

        private val tf: TextField = TextField()

        override fun startEdit(value: Any?, format: String, vararg options: Any) {
            if (value is String || value == null) {
                tf.text = value as String
            }
            attachEnterEscapeEventHandler()
            tf.apply {
                requestFocus()
                selectAll()
            }
        }

        private fun attachEnterEscapeEventHandler() {
            tf.onKeyPressed = EventHandler { t ->
                if (t.code == KeyCode.ENTER) {
                    endEdit(true)
                } else if (t.code == KeyCode.ESCAPE) {
                    endEdit(false)
                }
            }
        }

        override fun getEditor(): TextField = tf

        override fun getControlValue(): String = tf.text

        override fun end() {
            println("end")
            tf.onKeyPressed = null
        }
    }

Type


    inner class StringCellType(
        private val map: MutableMap<String, Any>,
        private val columnName: String
        ) : SpreadsheetCellType<String>(DefaultStringConverter()) {

        fun createCell(row: Int, column: Int, rowSpan: Int, columnSpan: Int, value: String): SpreadsheetCell {
            return StringCell(row, column, rowSpan, columnSpan, this).apply {
                item = value
            }
        }

        override fun createEditor(view: SpreadsheetView): SpreadsheetCellEditor {
            return StringCellEditor(view, columnName, map)
        }

        override fun toString(item : String): String = converter.toString(item)

        override fun toString(): String = "string"

        override fun match(value: Any?, vararg options: Any?): Boolean = true

        override fun convertValue(value: Any?): String? {
            val convertedValue = converter.fromString(value?.toString())
            return if (convertedValue == null || convertedValue == "") {
                null
            } else convertedValue
        }
    }

NPE:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.beans.value.ChangeListener.changed(javafx.beans.value.ObservableValue, Object, Object)" because "<local3>[<local7>]" is null
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:78)
    at javafx.graphics/javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:8129)
    at javafx.graphics/javafx.scene.Node.setFocused(Node.java:8182)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.setWindowFocused(Scene.java:4057)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.lambda$new$0(Scene.java:4079)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:136)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:78)
    at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics/javafx.stage.Window.setFocused(Window.java:702)
    at javafx.graphics/javafx.stage.Window$1.setFocused(Window.java:160)
    at javafx.graphics/com.sun.javafx.stage.WindowHelper.setFocused(WindowHelper.java:112)
    at javafx.graphics/com.sun.javafx.stage.WindowPeerListener.changedFocused(WindowPeerListener.java:64)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:126)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$4(GlassWindowEventHandler.java:178)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:176)
    at javafx.graphics/com.sun.glass.ui.Window.handleWindowEvent(Window.java:1248)
    at javafx.graphics/com.sun.glass.ui.Window.notifyFocus(Window.java:1227)

I tried to use the code of Controlfx in my project, but nothing effective.

By the way this project is a kotlin project with Controlfx. I want try to use javafx and java library to build some tools.

0

There are 0 best solutions below