11

I'm trying to load images from URL into girdview using Volley Library. For this I'm following this tutorial. When I run my project, it collects all URLs from server database and then store in arraylist. But doesn't displays images. And I am getting java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference error. It doesn't even point to any line number of code where I'm getting this error. See below Logcat Output :

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at com.android.volley.Request.<init>(Request.java:136)
        at com.android.volley.toolbox.ImageRequest.<init>(ImageRequest.java:71)
        at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:219)
        at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:171)
        at com.android.volley.toolbox.NetworkImageView.loadImageIfNecessary(NetworkImageView.java:141)
        at com.android.volley.toolbox.NetworkImageView.onLayout(NetworkImageView.java:190)
        at android.view.View.layout(View.java:15596)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.GridView.setupChild(GridView.java:1542)
        at android.widget.GridView.makeAndAddView(GridView.java:1436)
        at android.widget.GridView.makeRow(GridView.java:361)
        at android.widget.GridView.fillDown(GridView.java:302)
        at android.widget.GridView.fillFromTop(GridView.java:437)
        at android.widget.GridView.layoutChildren(GridView.java:1276)
        at android.widget.AbsListView.onLayout(AbsListView.java:2148)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.widget.ScrollView.onLayout(ScrollView.java:1502)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Can anybody help me what is the exact problem and how do I solve this ?

Ruchir
  • 1,086
  • 4
  • 24
  • 48
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Oct 13 '15 at 11:36
  • 1
    @Selvin No..This is not duplicate of What is Null pointer.... – Ruchir Oct 13 '15 at 11:38
  • 2
    You're calling #hashCode() on a null string. – nbokmans Oct 13 '15 at 11:42
  • 2
    @nbokmans why are you so sure that *he/she* calls it? I don't think the package name of his/her code would be `com.android...` – Sweeper Oct 13 '15 at 12:41

9 Answers9

10

You should check the null values for the image where you are getting the data from the server.

Avanish Singh
  • 278
  • 3
  • 9
5

I had the same error while trying to load a URL to the webview client in a fragment from a data class.
I wrote my own implementation of the hashcode function to check for null or empty responses from the API.
Here is the implementation.

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(url.isNullOrEmpty()){
          result = 31 * result + url.hashCode()
      }
        return result
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Peter Gichia
  • 171
  • 2
  • 5
4

Getting string value as null, for this instance shows String.hashcode() error

Gopi Cg
  • 384
  • 3
  • 7
0

I had same problem. Just found a fix I think. In the link to the server you are trying to communicate with, specify the protocol for communication i.e "http://" or "https://" before adding the domain and all. That fixed it for me. I hope it helps

Ata
  • 31
  • 3
0

I had this Error in another Content and fixed it by updating the Android SDK in Android Studio. Maybe it's helping.

Kay
  • 21
  • 2
0

My server is https and so was getting a different error. I removed the https and remained with the domain name only and it failed to work so I ended up putting up the https: and contacting my host to ease the security rules on the domain or create special provision for the specific domain. Check where you implement your site url and add http or https depending on how your site looks on browser.

0

It is a very good possibility that the data model you are mapping the API response to is expecting a non-null String parameter that is missing entirely or null. This can be because you typed the param name incorrectly or the API assumes the param is optional.

DevinM
  • 1,112
  • 1
  • 12
  • 29
0

Object Source has two parameters, id and name, then id allways is null, so I assigned "0" when use it method onItemClickListener, thats it works for me

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '22 at 20:47
0

When you get the data from the server and save it into an object, then call a log function to see all the data provided by API. After that, change all the values from null to empty strings and 0s.

onItemClickListener?.let {
                    article.source.id = ""
                    article.id = 0
                    article.author = ""
                    Log.i("News adapter ", article.toString())

                    it(article)
                }

That worked for me!

Navneet
  • 77
  • 6