-4

I am starting to use Kotlin alongside with Java. This below code in java. Throws no null pointer exception

 initFoodQuantitySpinner();

    CartDb cartDb = new CartDb(getActivity());

    try {
        P.addToCart(getActivity(), food.getId(), food.getPrice(), foodQuantity);
    } catch (Exception e) {
        e.printStackTrace();
    }

    FoodCartModel foodCartModel = new FoodCartModel(new Food(food.getId(),
            food.getName(), 0, "", "",
            food.getPrice(), 0,
            0, null, "", "", null));

    cartDb.insertFood(foodCartModel);
    if (getActivity() != null)
        getActivity().invalidateOptionsMenu();

This below code in Kotlin throws null pointer exception.

spinFoodQuantity.visibility = View.VISIBLE
    val cartDb = CartDb(context)

    val id = food.id
    val name = food.name
    val price = food.price
    try {
        P.addToCart(activity, id, price, foodQuantity)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    val foodCartModel = FoodCartModel(
            Food(id, name, 0, "", "", price, 0,
                    0, null!!, "", "", null!!),
            foodQuantity)

    cartDb.insertFood(foodCartModel)
    activity!!.invalidateOptionsMenu()

Add to cart function in shared pref

  public static synchronized void addToCart(Context context, int id, int price, int quantity)
        throws JSONException {
    assurePrefNotNull(context);
    String data = getCartData(context);
    JSONArray array;

    if (data.equals(""))
        array = new JSONArray();
    else array = new JSONArray(data);

    JSONObject item = new JSONObject();

    item.put("id", id);
    item.put("price", price);
    item.put("quantity", quantity);

    array.put(item);

    prefsEditor.putString(PREF_CART_DATA, array.toString());
    prefsEditor.commit();
}

Can anyone explain where is the wrong that kotlin throws null pointer exception? and android studio shows warning about unreachable code (for below code in kotlin)

  val foodCartModel = FoodCartModel(
            Food(id, name, 0, "", "", price, 0,
                    0, null!!, "", "", null!!),
            foodQuantity)

    cartDb.insertFood(foodCartModel)
    activity!!.invalidateOptionsMenu()
Ashiqul Islam
  • 147
  • 2
  • 12

1 Answers1

4

!! in Kotlin throws a KotlinNullPointerException if the marked field is null. For an instance:

var n: String? = null;
//changes to n based on whatever; not at all relevant. Should it end up as null afterwards
println(n!!)//throw an exception when something is done with it

Will throw an exception if n is null.

Meaning when you do null!! it will always throw an exception, since null is obviously null and can never have an actual value.

And since it always will throw an exception, the code below will be unreachable. It's the same as:

var n: String = "some string"
throw RuntimeException();//different exception obviously, but the idea is the same
println(n);//unreachable code

If you can have nullable arguments, add @Nullable to the field in Java or ? after the type in Kotlin to allow the arguments to be held as null. I mention both as you have both Java and Kotlin code in the question, but you didn't add the Food class. If you don't allow nullable arguments, don't pass null in the first place. And definitively not with non-null assertion

Zoe
  • 27,060
  • 21
  • 118
  • 148