Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Getting Data from an Intent

Robin Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Robin Wilson
iOS Development Techdegree Student 2,415 Points

Why (mName == null = false) when mName equals "" in debugger

when I left the EditText in the mainactivity blank , the if statement can not be triggered. I was curious about that and added a break point at if (mName == null) {. I found out that although the value of mName is "", mName == null = false. Can someone help me out of this?

package com.example.administrator.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import com.example.administrator.interactivestory.R; import com.example.administrator.interactivestory.model.Page; import com.example.administrator.interactivestory.model.Story;

public class StoryActivity extends AppCompatActivity {

private Story mStory = new Story();
private ImageView mImageView ;
private TextView mTextView;
private Button mButton1;
private Button mButton2;
private String mName;
private Page mCurrentPage;

private static final String TAG = StoryActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);
    Intent intent = getIntent();
     mName = intent.getStringExtra(getString(R.string.key_name));

    if (mName == null) {
        mName = "Friend";
    };
    Log.d(TAG,mName);

    mImageView = (ImageView) findViewById(R.id.storyImageView);
    mTextView = (TextView) findViewById(R.id.textView);
    mButton1= (Button) findViewById(R.id.choiceButton1);
    mButton2= (Button) findViewById(R.id.choiceButton2);


    loadPage(0);


}


private void loadPage(int choice){
    mCurrentPage = mStory.getPage(choice);

    Drawable drawable = ResourcesCompat.getDrawable(getResources(),mCurrentPage.getImageId(),null);
    mImageView.setImageDrawable(drawable);

    String pageText = mCurrentPage.getText();
    pageText=String.format(pageText,mName);
    mTextView.setText(pageText);
    if (!mCurrentPage.getFinal()) {
        mButton1.setText(mCurrentPage.getChoice1().getText());
        mButton2.setText(mCurrentPage.getChoice2().getText());
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadPage(mCurrentPage.getChoice1().getNextPage());
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadPage(mCurrentPage.getChoice2().getNextPage());
            }
        });
    } else
     {
         mButton2.setText("play again");
         mButton1.setEnabled(false);
         mButton1.setVisibility(View.INVISIBLE);
         mButton2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 finish();
             }
         });
    }
}

private void endStory(){
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

}

4 Answers

inventor02
inventor02
2,027 Points

I know that you have fixed the problem, but I'll explain why this happened."" is not equal to null because "" is still a String, but it does not contain any characters. null, however, is equal to nothing. Not an empty string, literally nothing. You can think of it like a room. "" is an empty room, and null is a room that simply doesn't exist. The empty room still exists, but there's just nothing in it. However, the room that never existed simply isn't there and never will be there until you create it. Just a side-pointer as well: when you create a variable and don't assign it is equal to null.

Button mButton;

At this stage, mButton is equal to null because I have not yet assigned anything to it.

mButton = (Button) findViewById(R.id.button);

At this stage, mButton is equal to the Button in my activity with the ID button.

Hope this helps :)

Ron Jones
Ron Jones
4,548 Points

This also works on a null. if (TextUtils.isEmpty(name)) { name = "Freind"; }

inventor02
inventor02
2,027 Points

No problem, happy to help :D