Java: Exception itself is null

℡╲_俬逩灬. 提交于 2019-11-27 14:57:30
Stephen C

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 Davlik 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.


FWIW, 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 SO question: Exception is NULL always

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.

You're probably being fooled by your debugger.

Add another line of code under that line (something unuseful like if(false) log.v("","");), break there and check from there the value of your exception.

Also try Log.e(TAG, "my null exception", e); and read the log.

Swordfish90

I'm not sure if it's the same problem but I've already answered a similar question... If it happens when you try to read from an InputStream then take a look at this link...

Android Bluetooth connection issue

jayunit100

In Java, you can throw a NullPointerException by throwing null. In this case, the throwable will ultimately be a NullPointerException.

public static void main(String[] args)
    {
        try{
            a(null);
        }
        catch(Exception e)
        {
            System.out.println(e);
            e.printStackTrace();
        }
    }
    public static void a(String[] args)  
    {
        throw null; 
    }

Output:

java.lang.NullPointerException 
java.lang.NullPointerException  
at scjp.Scjp.a(Scjp.java:18)    
at scjp.Scjp.main(Scjp.java:8)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!