title: Populating UI with JSON
body: <p>I need help. I'm doing some project for a review and I'm stuck with populating UI with JSON.</p>
  
  <p>Here's the code for JsonUtils.class</p>
  
  <pre><code>package com.udacity.sandwichclub.utils;
  
  import com.udacity.sandwichclub.model.Sandwich;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class JsonUtils {
  
      public static Sandwich parseSandwichJson(String json) {
  
          try {
              //Root JSONObject Sandwich
              JSONObject jsonSandwich = new JSONObject(json);
  
              //Sandwich common name and also known as
              JSONObject sandwichName = jsonSandwich.getJSONObject("name");
  
              //Common name of the sandwich
              String commonName = sandwichName.getString("mainName");
  
              //Sandwich is also known as
              List&lt;String&gt; alsoKnownAsName = jsonArrayList(jsonSandwich
                      .getJSONArray("alsoKnownAs"));
  
              //Place of origin of the sandwich
              String placeOfOrigin = jsonSandwich.getString("placeOfOrigin");
  
              //Description of the sandwich
              String descript = jsonSandwich.getString("description");
  
              //Image of the sandwich
              String sandwichImg = jsonSandwich.getString("image");
  
              //Ingredients of the sandwich
              List&lt;String&gt; sandwichIngredients = jsonArrayList(jsonSandwich
                      .getJSONArray("ingredients"));
  
              //Return object Sandwich
              return new Sandwich(commonName, alsoKnownAsName, placeOfOrigin,
                      descript, sandwichImg, sandwichIngredients);
  
          } catch (JSONException e) {
              e.printStackTrace();
              return null;
          }
      }
  
      //Method for listing ingredients and also known as names
      public static List&lt;String&gt; jsonArrayList(JSONArray jsonArray) throws JSONException {
          List&lt;String&gt; jsonList = new ArrayList&lt;String&gt;();
  
          if(jsonArray != null) {
              for(int i=0; i &lt; jsonArray.length(); i++) {
                  jsonList.add(jsonArray.getString(i));
              }
          }
  
          return jsonList;
      }
  }
  </code></pre>
  
  <p>and here's detail activity which has to receive JSON data.
  DetailActivity.java</p>
  
  <pre><code>package com.udacity.sandwichclub;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.squareup.picasso.Picasso;
  import com.udacity.sandwichclub.model.Sandwich;
  import com.udacity.sandwichclub.utils.JsonUtils;
  
  public class DetailActivity extends AppCompatActivity {
  
      public static final String EXTRA_POSITION = "extra_position";
      private static final int DEFAULT_POSITION = -1;
  
      private Sandwich sandwich;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_detail);
  
          ImageView ingredientsIv = findViewById(R.id.image_iv);
  
          Intent intent = getIntent();
          if (intent == null) {
              closeOnError();
          }
  
          int position = intent.getIntExtra(EXTRA_POSITION, DEFAULT_POSITION);
          if (position == DEFAULT_POSITION) {
              // EXTRA_POSITION not found in intent
              closeOnError();
              return;
          }
  
          String[] sandwiches = getResources().getStringArray(R.array.sandwich_details);
          String json = sandwiches[position];
          Sandwich sandwich = JsonUtils.parseSandwichJson(json);
          if (sandwich == null) {
              // Sandwich data unavailable
              closeOnError();
              return;
          }
  
          populateUI();
          Picasso.with(this)
                  .load(sandwich.getImage())
                  .into(ingredientsIv);
  
          setTitle(sandwich.getMainName());
      }
  
      private void closeOnError() {
          finish();
          Toast.makeText(this, R.string.detail_error_message, Toast.LENGTH_SHORT).show();
      }
  
      private void populateUI() {
          //Populating also known as text with JSON
          TextView alsoKnownAsText = (TextView) findViewById(R.id.also_known_tv);
          alsoKnownAsText.setText(sandwich.getAlsoKnownAs() + ", ");
  
          //Populating ingredients text with JSON
          TextView ingredientsText = (TextView) findViewById(R.id.ingredients_tv);
          alsoKnownAsText.setText(sandwich.getIngredients() + ", ");
  
          //Populating place of origin text with JSON
          TextView placeOfOriginText = (TextView) findViewById(R.id.origin_tv);
          placeOfOriginText.setText(sandwich.getPlaceOfOrigin());
  
          //Populating description text with JSON
          TextView descriptionText = (TextView) findViewById(R.id.description_tv);
          descriptionText.setText(sandwich.getDescription());
      }
  }
  </code></pre>
  
  <p>So this method populateUI dosen't work for me ... when I click from MainActivity one item it pop's up an toast message that there's no data for that.</p>
  
  <p>This is what Logcat says:</p>
  
  <pre><code>02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err: org.json.JSONException: No value for alsoKnownAs
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at com.udacity.sandwichclub.utils.JsonUtils.parseSandwichJson(JsonUtils.java:28)
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at com.udacity.sandwichclub.DetailActivity.onCreate(DetailActivity.java:42)
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.Activity.performCreate(Activity.java:6662)
  02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.os.Looper.loop(Looper.java:154)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
  02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  </code></pre>