wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null Pointer Exception ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null Pointer Exception ???" Watch "Null Pointer Exception ???" New topic
Author

Null Pointer Exception ???

Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
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

Joined: Dec 05, 2000
Posts: 17
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

Joined: May 06, 2000
Posts: 396
Hi Praveen,
This code is perfect. It should run and is running.
what problem r u facing?
regards
Deekasha
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
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

Joined: Nov 10, 2000
Posts: 6
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

Joined: Sep 28, 2000
Posts: 199
Hi all,
The output I am getting is a number like 1934181044 , which is the hashcode.
Aruna
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
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

Joined: Sep 23, 2000
Posts: 243
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).
 
GeeCON Prague 2014
 
subject: Null Pointer Exception ???