• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointer Exception

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why below given prg throws Nullpointer exception


[HENRY: Added Code Tags. Formatted Code.]
[ December 20, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, because you are calling method on null reference of Test .
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ClassA
{
public void methodA()
{
ClassB classB = new ClassB();
classB.getValue();
//System.out.println(" ClassA's methodA()!");
}
}
class ClassB
{
public Test classC=new Test();
public String getValue()
{
//System.out.println("ClassB's getValue() !");
return classC.getValue();

}
}
public class Test
{
public String value="ss";
public String getValue()
{
value = "ClassB";
// System.out.println("Test's getValue()!");
return value;

}
public static void main(String s[])
{
ClassA a = new ClassA();
a.methodA();
// System.out.println("main mothods()!");
}
}

Look at "public Test classC=new Test(); ",if I write this, there is no errors! Compilation successfully! So I think, the object classC must be instantiate. This is my own standpoint!
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ClassB { public Test classC; public String getValue() { return classC.getValue(); }}

In the above ClassB,ur statement is:
public Test classC;-------Here classC contains only null value.
Then only NullPointerException is occurring.

so
for avoid this,
we have two options:
1. we need to use statement like this,
Test classC=new Test();-----Now classC having details of Test class.
or
2. we've to change the class Test like this.

public class Test
{
public static String value="ss";
public static String getValue()
{
value = "ClassB";
return value;
}
public static void main(String s[])
{
ClassA a = new ClassA();
a.methodA();
}
}

Because static methods and static variables are class level variables.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always keep in mind, for any non-primitive objects, default value will NOT be given, and you must NEW it before use.

Nick
 
reply
    Bookmark Topic Watch Topic
  • New Topic