0

This is my TYPEFACE class

public class TYPEFACE {

    public static final Typeface Rupee(Context ctx){
        Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "Rupee_Foradian.ttf");
        return typeface;
    }
    public static final Typeface ArialRounded(Context ctx){
        Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "Rupee_Foradian.ttf");
        return typeface;
    }

}

And I try to access the methods from this class using Reflection by the following way

Method method;
TYPEFACE typeface;
try{            
    method = typeface.getClass().getMethod("Rupee",Context.class);
}catch(Exception e){
    System.out.println("method : "+e);
}

But it throws the nullpointerexception.

05-21 17:12:07.026: I/System.out(14917): method : java.lang.NullPointerException

I dont know What is wrong with this way. Sombody can help me please.

Thanks in advance.

Mike Nakis
  • 50,434
  • 8
  • 92
  • 124
Gunaseelan
  • 12,697
  • 10
  • 74
  • 118

3 Answers3

4

typeface is null, because it's not initialized

Change this to TYPEFACE typeface = new TYPEFACE();

Vincent van der Weele
  • 12,682
  • 1
  • 31
  • 60
2

The NullPointerException is due to the variable typeface being null.

But you don't need an instance of TYPEFACE class to invoke that method. Simply try

try{            
    method = TYPEFACE.class.getMethod("Rupee",Context.class);
    method.invoke(null, new Context()); // since its a static method, can be invoked directly
}catch(Exception e){
    System.out.println("method : "+e);
}
sanbhat
  • 17,162
  • 6
  • 47
  • 63
0

you want to initialize the typeface then after use it so

you second code is like....

Method method;
TYPEFACE typeface=new TYPEFACE();
try{            
    method = typeface.getClass().getMethod("Rupee",Context.class);
}catch(Exception e){
    System.out.println("method : "+e);
}
Jatinkumar Patel
  • 1,693
  • 2
  • 15
  • 35