Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

android - Java: Exception itself is null

I am pretty confused. Is it an Android problem or a problem with Java itself?

When I was debugging an Android application which works with Bluetooth, the flow stopped on an catch block of IOException in which I later found out that exception e was null.... It was thrown when I was trying to read from an InputStream.

Yes, it was not a NullPointerException, but some other kind of exception which is null - better say thrown un-initialized.

Is it possible? In which scenario can such unitialized exceptions be thrown?

An exception is null... Note: it's NOT a null pointer exception!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is it possible? In which scenario such unitialized exceptions can be thrown??

This is not possible using a conformant Java compiler and conformant Java virtual machine, and by extension a conformant Dalvik virtual machine. The JLS doesn't allow the e variable to be null at that location

Either you've got a buggy virtual machine, a buggy debugger, or a problem with your IDE, build tools and / or process.

If I was in your situation, I'd stop using the debugger for now, and fall back on adding old-fashioned traceprints to your code. And make sure that you do a clean and full build from source.


One other possibility you should consider is that the line numbers that the JRE is reporting at runtime (and that the debugger is relying on) are not lining up with the line numbers in the source code. This could happen if you've made a mistake in your build and deployment processes. The mistake might be something like forgetting to save a file, forgetting to build, forgetting to deploy the new version of the app or getting your IDE out of sync with the filesystem.


For what it's worth, the theory is that this is caused by throw null; or something equivalent doesn't hold water. The JLS section 14.18 says:

"If evaluation of the Expression completes normally, producing a null value, then an instance V' of class NullPointerException is created and thrown instead of null."

It is easier to understand if you read that sentence in its context, but it is saying clearly that throw null; actually throws a NullPointerException.


UPDATE

I found another plausible explanation in this Stack Overflow question: Exception is always NULL

Basically, it is saying that the emulated code is throwing an exception that Eclipse doesn't know about, and the Eclipse emulator is "helpfully" substituting a null. That sounds like an emulator bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...