• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception ???

 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain the behaviour of the following code ?
class AClass {
public int xvar;
static AClass createA() {
return new AClass();
} // createA()
} // AClass
public class Tester {
public static void main(String[] parms) {
AClass aVar = null;
System.out.println(aVar.createA().hashCode());
}
} // Tester
TIA
Praveen Zala
 
vishad patel
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friend
in this programm ,in main mehtod you declare one variable of Aclass type which is set null,it mean it can not yet point to any object.so that when you try to run this programme it will generate NullPointer Exception at run time because you try to acess variable before initialise it .
thank for posting your problem.
bye.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,
This code is perfect. It should run and is running.
what problem r u facing?
regards
Deekasha
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen,
Look at the following code
public class Tester
{

public Tester()
{
}
public static void main(String[] args)
{
Tester T = new Tester();
Integer aVar = null;
System.out.println(aVar.byteValue());
// System.out.println(aVar.hashCode());
System.out.println(T.hashCode());

}
}
in here it throws the exception while trying to get the byteValue(), simply becasue Integer aVar = null does not create any object, but tells the compiler that aVar is of the reference type Integer and currently it is not refering to any objects.
Thus it has no way to call byteValue().
Hope this helps.
 
Kirti Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is fine.
Had method ACreate() no been declared static, then exception
would have been thrown b'coz null objects have runtime values/access to static variables/methods only.
K Singh
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The output I am getting is a number like 1934181044 , which is the hashcode.
Aruna
 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am thankful for the enthusiasm....
What I dont understand in the above code is
why a null pointer exception is NOT thrown ?
Can someone elaborate with an example where a
Null Pointer Exception is thrown ?
Praveen Zala
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the explaination from JLS:

The following example demonstrates that a null reference may be used to access a class (static) variable without causing an exception:
class Test {
static String mountain = "Chocorua";
static Test favorite(){
System.out.print("Mount ");
return null;
}
public static void main(String[] args) {
System.out.println(favorite().mountain);
}
}
It compiles, executes, and prints:
Mount Chocorua
Even though the result of favorite() is null, a NullPointerException is not thrown. That "Mount " is printed demonstrates that the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic