A question in Java

Discussions

EJB design: A question in Java

  1. A question in Java (3 messages)

    Hi,

    I've a basic question in Java. If i have a code like

    String str = null;
    System.out.println(str.length);

    The above code will throw a nullpointer exception. Is there a way to find out that str is the variable that threw the null pointer exception?

    Thanks,
    Eshwari

    Threaded Messages (3)

  2. use:
    if(str!= null) {
    System.out.println(str.length);
    } else {
    System.out.println("str is nul");
    }

    or, use assertions, or maybe you dont have to know that str is null.
  3. you can use JDB utility of JAVA to debug the java code properly. Hope this iwll help

    abhijeet mahalkar
  4. Hi,I've a basic question in Java. If i have a code likeString str = null;System.out.println(str.length);The above code will throw a nullpointer exception. Is there a way to find out that str is the variable that threw the null pointer exception?Thanks,Eshwari

    The exception trace stack should point to the line of your code that did generate this NPE. In most cases, this should be sufficient to figure out the problem...