public class Wells {
String str[] = null;
static public void main(String args[]) {
Wells objWells = new Wells();
System.out.println(args.length);// we will NOT get null pointer exception WHY ?
System.out.println(objWells.str.length); // we will get null pointer exception
}
}
Asked
Active
Viewed 77 times
0
-
The `str[]` for a new Wells is null, so you get an NPE when you try to get its length. For the "why is `args` _not_ null" portion, see http://stackoverflow.com/questions/9605532/args-guaranteed-to-be-non-null – yshavit Jan 10 '17 at 07:13
-
The question would be, why would you expect `str` to be anything else than `null`. As for `args`, it will never be by default – AxelH Jan 10 '17 at 08:22
1 Answers
0
In a main method, the argument is never null.
In your object you are defining it as null, so it will be null

SCouto
- 7,808
- 5
- 32
- 49
-
-
Well, i'm not sure but AFAIK the args array is initialized by the JVM so it's always initialized and never null. If you need to check its emptyness you should check if args.length == 0 – SCouto Jan 10 '17 at 09:46