0

Is it possible to return the variable name in java?

If so, how do you do it?

(I am using IntelliJ Community Edition)

EDIT:

Comment below answered about the line of where the error is. I never realize that the function name line was actually for the exact line and not for the start of the function.

4
  • The linenumber is buried in the stacktrace (Line: XXX). To get the variablename, at least for NullPointerExceptions, you have to pass -XX:+ShowCodeDetailsInExceptionMessages to the JVM Commented Aug 15, 2020 at 17:13
  • I have only being receiving the function name and line (not the exact line within the function). How do I do the later part? Commented Aug 15, 2020 at 17:15
  • EDIT: The function line was actually pointing to the exact line within the function. I am such an idiot. I still am curious on how to do the second part though. Commented Aug 15, 2020 at 17:18
  • Please refer to my answer Commented Aug 15, 2020 at 17:21

1 Answer 1

1

Suppose this code:

public class Test{
    public static void main(String[] args){
        try{
            throw new IllegalArgumentException();
        }catch(IllegalArgumentException iae){
            iae.printStackTrace(); //Get stacktrace
        }
        String s=null;
        System.exit(s.length()); //Boom
    }
}

If you run it with java Test, you get this output:

java.lang.IllegalArgumentException
    at Test.main(Test.java:4) //Here, the line
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:9) //Here, the line

If you run it with java -XX:++ShowCodeDetailsInExceptionMessages Test, you get this output:

java.lang.IllegalArgumentException
    at Test.main(Test.java:4)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null
    at Test.main(Test.java:9) //You get the line and what is null, here a local with no name

If you compile with javac -g Test.java, and run it, you even get more output:

java.lang.IllegalArgumentException
    at Test.main(Test.java:4)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
    at Test.main(Test.java:9)

It doesn't work for other exceptions, but a NPE is thrown quite often.

-XX:+ShowCodeDetailsInExceptionMessages will only work in Java 14+!

Sign up to request clarification or add additional context in comments.

7 Comments

@Michael That's correct. Oops, I forgot that to mention :-)
Ah shucks. My (uni) project requires me to use Java 8. Ag man. THANK YOU though, I will use this in the future when I eventually use newer Java versions. I really appreciate it :)
@Arneev, this has been (and continues to be) a major issue for Java developers literally as long as the language has existed. Sadly, it is not a given you will encounter only up-to-date Java "out in the wild" - even Java 8 is not universally adopted yet.
Well, you can write Java 8 code and run on Java 14 JVM, and you will still benefit from the above. Since JVMs maintain backwards compatibility, there is very little reason not to upgrade your JVM.
@Michael, in an enterprise scenario your JVM might be part of your application server. And upgrading application servers is definitely not something you do in a coffee break.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.