Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference #44

Open
AkanshChoudhary opened this issue Sep 22, 2022 · 2 comments

Comments

@AkanshChoudhary
Copy link

Getting this error when Navigating from breaking news screen to article screen

@Fr4nc1e
Copy link

Fr4nc1e commented Nov 28, 2022

Getting this error when Navigating from breaking news screen to article screen

Same issue, have you fixed this?

@Fr4nc1e
Copy link

Fr4nc1e commented Nov 28, 2022

I have solved this by rewriting the hashCode() function like this. I hope it can help you and anyone else.

package com.mvvm.newspaper.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.io.Serializable

@Entity(
    tableName = "articles"
)
data class Article(
    @PrimaryKey(autoGenerate = true)
    var id: Int? = null,
    val author: String,
    val content: String,
    val description: String,
    val publishedAt: String,
    val source: Source,
    val title: String,
    val url: String,
    val urlToImage: String
) : Serializable {

    override fun hashCode(): Int {
        var result = id.hashCode()
        if (content.isEmpty()) {
            result = 31 * result + content.hashCode()
        }
        return result
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Article

        if (id != other.id) return false
        if (author != other.author) return false
        if (content != other.content) return false
        if (description != other.description) return false
        if (publishedAt != other.publishedAt) return false
        if (source != other.source) return false
        if (title != other.title) return false
        if (url != other.url) return false
        if (urlToImage != other.urlToImage) return false

        return true
    }
}
```
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants