1

I'm trying to modify an Android app that is pulling some JSON from a server..

JSONObject mResult = new JSONObject((String) result);

Then using that to construct an object from my Sponsor class..

Gson mGson = new Gson();
mSponsor = mGson.fromJson(mResult.getJSONObject("data").getJSONObject("sponsor").toString(), Sponsor.class);

Sponsor class is just a shell with setters and getters, no logic done. Something like this:

public class Sponsor {

    @SerializedName("address1")
    private String Address1;
    @SerializedName("address2")
    private String Address2;

    public Sponsor(String address1, String address2) {
        Address1 = address1;
        Address2 = address2;
    }

    public Sponsor() {
        Address1 = "";
        Address2 = "";
    }

    public String getAddress1() {
        return Address1;
    }

    public void setAddress1(String address1) {
        Address1 = address1;
    }

    public String getAddress2() {
        return Address2;
    }

    public void setAddress2(String address2) {
        Address2 = address2;
    }
}

The app puts the address in a TextView like so:

txtAddress = (TextView) view.findViewById(R.id.txt_sponsor_address);
txtAddress.setText(mSponsor.getAddress1() + "\n" + mSponsor.getAddress2());

This all works, except when the server returns null for address2, in which case the literal text "null" is printed in the TextView where address2 should be.

My question is how can i get rid of the "null"?

I've tried a few different things, started with a simple ternary statement and got more and more verbose and nothing has worked so for.

Here is the latest and most ridiculous-looking thing I've tried so far:

String addr2 = ((String) mSponsor.getAddress2()).trim();
String a2;
if((addr2 == "null") || (addr2 == "")) a2 = (String) "";
else a2 = (String) addr2;

txtAddress.setText(mSponsor.getAddress1() + "\n" + a2);

This causes the app to crash when I open that Activity.

I'm a noob at Java so please provide references if you can.

1
  • Have you tried addr2.equals("null") or addr2 == null? Commented Jun 3, 2016 at 15:13

6 Answers 6

3

"getAddress2()" isn't returning the literal string "null". It's returning a null reference and the StringBuilder is converting it to a "null" String when you print it in the concatenation. That's why it crashes when you try to trim the word.

A simple way to fix it is to put the ternary operator in the getter itself like so:

public String getAddress2() {
  return (Address2 == null) ? "" : Address2;
}

Now you'll never return a null reference and you can safely use it without any checks.

Sign up to request clarification or add additional context in comments.

4 Comments

Maybe this is a separate question, but wouldn't the public **String** part guarantee that a String is returned?
It guarantees that an object that is returned will implement a String interface. That means, if it exists, you can perform the String methods on said object. The object that is returned may not actually exist though (hence null). If the object doesn't exist, then you can't perform operations on it. Java doesn't have any way of guaranteeing that a non-null is returned. You have to guarantee that through the interface because sometimes a null is appropriate. For example, in this scenario, how do you know the server returned a "null" and not an empty string? You do lose that information.
Most commonly though you would return a default object like an empty String so you don't have to sprinkle "if(obj != null)" everywhere in the code (that gets old really, really fast).
Thank you for helping me understand. I'm now comfortable with using this solution.
3

You could create another getter in your Sponsor class, where you'd put your logic to get a formatted address :

public String getFormattedAddress() {
    if (!TextUtils.isEmpty(Address1) && !TextUtils.isEmpty(Address2)
        && !Address1.equalsIgnoreCase("null") && !Address2.equalsIgnoreCase("null")) {
      return (Address1 + "\n" + Address2);
    }
    if (!TextUtils.isEmpty(Address1) && !Address1.equalsIgnoreCase("null")) {
      return (Address1);
    }
    if (!TextUtils.isEmpty(Address2) && !Address2.equalsIgnoreCase("null")) {
      return (Address2);
    }
    return ("");
}

And then simply setText with :

txtAddress.setText(mSponsor.getFormattedAddress());

3 Comments

Thanks for the answer, I get most of what you're doing, pretty self-explanatory, but can you please briefly tell me what TextUtils is? Do I need to import that?
@PootieTang TextUtils is a utility class that's part of the Android SDK so you don't have to add it to the dependencies.
TextUtils is part of the android text imports, which checks for null or empty String. Simply import import android.text.TextUtils;
1

You should not use :

if((addr2 == "null")

For string comparaison use:

if (addr2.equalsIgnoreCase("null"))

Comments

1

How about this?

// if null, addr2="", if not null, addr = getAddress2()'s result 
String addr2 = mSponsor.getAddress2() == null ? "" : ((String) mSponsor.getAddress2()).trim();

txtAddress.setText(mSponsor.getAddress1() + "\n" + addr2);

Comments

1
String addr2 = mSponsor.getAddress2();
if(!TextUtils.isEmpty(addr2) || !addr2.equalsIgnoreCase("null")
     txtAddress.setText(mSponsor.getAddress1() + "\n" + addr2);
else
     txtAddress.setText(mSponsor.getAddress1());

Comments

0

Try this for your getter code:

 public String getAddress2() {
    if (Address2.equals("null")) {
        Address2="";
    }
    return Address2;
 }

Of course, you can do the same with your other getter for Address1. Also, if you want to check for an empty string, use Address2.length() == 0 as opposed to Address2 == "".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.