• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

inner class query

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

Could you please explain the following code why is it throwing NULL pointer exception at run time.

interface A{
public void innerMeth();
}
public class Test {
A a;
int memVar = 1;
void aMethod(){
a = new A(){
public void innerMeth(){
System.out.println(memVar);
} };
}

public static void main(String[] args){
Test t = new Test();
t.a.innerMeth();
}
}

Thanks in advance,
Phani
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not initializing the reference variable a or Interface 'A'. That's why it's throwing NullPointerException
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Within the method aMethod(). You are initializing a method-local inner Class A, but this class is only available within aMethod()

If you change your code in


it will print 1
 
Phani Raj Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramko, devashish

Thanks for your help. But if i put t.aMethod() before t.a.innerMeth() it works fine. I am thinking as the method innerMeth() is
enclosed in aMethod() we have to call aMethod() before calling innerMeth(). Please correct me if i am wrong.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly.

As Devashish Roy posted before, instance member a is only initialized inside aMethod. So, if you don't call that method, it will be null (default value) when you call innerMeth.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Preparing for SCJP 1.5.

Please look at the code below:
interface Cookable {
public void cook();
}
class Food {
Cookable c = new Cookable() {
public void cook() {
System.out.println("anonymous cookable implementer");
}
};
}

Here, I added a main method to class food

public static void main(String[] args) {
Food f = new Food();
f.c.cook(); //line 1
}

I was trying to call cook method but there is a compiler error on line1 saying there is no definition matching cook().

Can somebody tell me how can I call method cook, then.

Please reply.

Thanks.
 
Phani Raj Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi reet,
try this out

Hi,

Try this code

interface Cookable {
public void cook();
}
class Food {
Cookable c =new Cookable() {
public void cook() {
System.out.println("anonymous cookable implementer");
}
};


public static void main(String[] args) {
Food f = new Food();
f.c.cook(); //line 1
}

}


But it compiles fine and even printed "anonymous cookable implementer". Let me know if you have query still.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic