0

I recently came across an exception while writing a program and it took me a lot of time to debug simply because I was given a wrong exception by the compiler.

Here's my activity code:

 private var mCheatMap = HashMap<Int, Boolean>()

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quiz)

    mCheatMap = savedInstanceState?.getSerializable(KEY_CHEATER) as HashMap<Int, Boolean> ?: HashMap<Int, Boolean>()
}

During runtime, my app crashed and when I looked into the logs, it said ActivityNotFound exception and in suggestion, it said maybe I didn't declare the activity in my AndroidManifest.xml

Everything seemed to be error free from my side when suddenly I, by chance, made my mCheapMap variable casting into a safe cast and everything started to work perfectly. e.g.:

 private var mCheatMap = HashMap<Int, Boolean>()

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quiz)

    mCheatMap = savedInstanceState?.getSerializable(KEY_CHEATER) as? HashMap<Int, Boolean> ?: HashMap<Int, Boolean>()
}

Now I have a few confusions:

  1. Why the compiler gave me the ActivityNotFound exception while I was having a casting issue?

  2. Why using the safe cast operator solved the problem because even without the safe cast operator, my casting was correct?

Benjamin
  • 7,055
  • 6
  • 40
  • 60
HQuser
  • 640
  • 10
  • 26
  • @user2340612 that's the issue, the exception I only get was ActivityNotFound exception which is really out of sense. P.S. thank you for clearing my second question. – HQuser Mar 14 '18 at 11:29
  • 1
    Maybe the `ActivityNotFound` might be explained by the fact that the `ClassCastException` is thrown in the Activity's `onCreate`, hence the creation does not succeed and the Activity cannot be found by the runtime. But that's only my guess, I may be wrong – user2340612 Mar 14 '18 at 11:33
  • This looks more like a null pointer exception than a class cast exception...I may be wrong. – Ishita Sinha Mar 14 '18 at 11:38
  • The OP said that using the safe cast solved the issue, which should not be the case for a NPE. Plus, with the safe call operator (`?.`) and the elvis operator, `null` values are handled "safely" (unless the NPE happens somewhere else) – user2340612 Mar 14 '18 at 11:41

1 Answers1

0

At runtime you should get a ClassCastException, if the issue is about the wrong casting (by the way it's not the compiler that throws the exception, but the runtime. The compiler/LINT should warn you about an "unchecked cast", though).

Concerning your second question, the safe cast will return null in case of an unsuccessful cast, which makes you fall in the "else" branch of the elvis operator (?:), so you will create a new empty HashMap<Int, Boolean> and assign it to your mCheatMap variable. That doesn't happen in your first example since the failing cast throws an exception instead of simply returning null, so the "else" branch of the elvis operator can never be executed (you should get another warning by the compiler/LINT for this).

user2340612
  • 10,053
  • 4
  • 41
  • 66