Tag Archives: Java

Picasso and null path to image

Found something very odd when programming my Android mobile app today. I am aware that I should not provide a null path for Picasso to load. Hence defensive statements in my code to make sure that Picasso would not get to load a null path. But somehow my app was still crashing, with the following error:

    java.lang.IllegalArgumentException: Path must not be empty

And the error was pointing to the line where the Picasso call was located:

Picasso.get().load(“null image path”);

Even when this line would not be run, which I have checked, it was still giving me problem. Turns out that my defensive statement allowed non-null image path to happen first, as below:

if (imgPath != null || !(imgPath.isEmpty()) {
    Picasso.get().load(imgPath).into(imageView);
} else { 
    Picasso.get().load(placeholder).into(imageView);
}

If I swap the defensive statement around, such that imgPath == null is checked first, then my app will run okay. Very odd. Anyone kind enough to explain to me what was that about?

Adding data to Firebase using custom objects

I’m fairly new to using Firebase, and their nosql type databases. It did take me some time to understand how data is arranged, with collections and documents.  When developing my application, in Java, I created a class for storing data, a.k.a. POJO. What is unclear from the Firebase documentation is how the fields in a document will be named.

There is a line that says this: Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property.

I initially did not pay attention to this public getter, i.e. a get method for each property in my object. Of course it is good practice to always create all the methods when creating a class. I was lazy, thinking that I only add them when I need to use them. Then came the interesting bit, where even after all the properties in my object had been initialised, and added to the Firebase document, some properties went missing in the database. I found it very odd. I initially thought that the fields in a Firebase document would be added according to my Java class, and following the naming I used in my class. But that was not the case. Dug a bit deeper, and it follows that if my getter method is getImgPath(), then the field in my Firebase document will become imgPath (note the upper case for “I” becomes “i”).  And without a getter method for property, then you will have a missing field in your Firebase document!

Hope this helps for those who are wondering how a document in Firebase is populated using custom objects.