简体   繁体   中英

java.lang.NumberFormatException: For input string:"something"

i tried to split a string to two variable, one is string and one is Long. it work, when i Log it i can see it and i can insert it to Room Database, but i don't know why "sometime" i get this error. the string like this

m8fw5sMdAcaX4Ezv7vzImeRAjkq2_1635234951781
java.lang.NumberFormatException: For input string: "m8fw5sMdAcaX4Ezv7vzImeRAjkq2"
        at java.lang.Long.parseLong(Long.java:594)
        at java.lang.Long.parseLong(Long.java:636)
        at com.animals.snowy.MainActivity$insertMessage$1$messageListener$1.onChildAdded(MainActivity.kt:88)

my Model

@IgnoreExtraProperties
@Entity(tableName = "message_table")
data class MessageModel(
    @NonNull
    @PrimaryKey
    var messageId: String = "",
    var messageType: String? = null,
    var messageTimestamp: Long? = null,
    var messageData: String? = null,
    var messageSenderId: String? = null,
    var roomId: String = "",
    var isSeen: Boolean = false,
var uploadSuccess : Boolean = false
) : Serializable {
}

and this is my code , i want to get new message of friends, so i get list friend from Room Database and use for loop to get roomId(name of child container message of me and my friend).

private fun insertMessage() {
        viewModel.readRoomIdFriendsDAO().observe(this, { listRoomId ->
            if (listRoomId != null && listRoomId.isNotEmpty()) {
                for (item in listRoomId) {
                    val messageListener = object : ChildEventListener {
                        override fun onChildAdded(snapshot:DataSnapshot,previousChildName: String?) {

                            val messageModel: MessageModel? =
                                snapshot.getValue(MessageModel::class.java)
                            if (messageModel != null) {
                                messageModel.messageId = snapshot.key.toString().trim()
                                messageModel.roomId = item
                                messageModel.uploadSuccess = true
                                val listTemp = messageModel.messageId.split("_")
                                messageModel.messageSenderId = listTemp[0]
                                messageModel.messageTimestamp = listTemp[1].trim().toLong()
                                Log.e(TAG,"senderId: ${messageModel.messageSenderId}")
                                Log.e(TAG,"timestamp: ${messageModel.messageTimestamp}")
//                                messageViewModel.insertMessageDAO(messageModel)
                            }
                        }
                        override fun onChildChanged(
                            snapshot: DataSnapshot,
                            previousChildName: String?
                        ) {

                        }
                        override fun onChildRemoved(snapshot: DataSnapshot) {

                        }

                        override fun onChildMoved(
                            snapshot: DataSnapshot,
                            previousChildName: String?
                        ) {
                            TODO("Not yet implemented")
                        }

                        override fun onCancelled(error: DatabaseError) {
                            TODO("Not yet implemented")
                        }
                    }
                    messageRef
                        .child(item)
                        .addChildEventListener(messageListener)
                }
            }
        })
    }

java.lang.NumberFormatException is thrown whenever the compiler tries to type cast an invalid input to number.

For example:

String validInputToCast = "123456789";
Long validInputToCast = validInputToCast.toLong(); // this casting will succeed.

String invalidInputToCast = "abce124";
Long invalidCastedInput = inputToCast.toLong(); // compiler will throw number exception on this line.

Try debugging the line below and hopefully you will find the error.

messageModel.messageTimestamp = listTemp[1].trim().toLong()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM