Home Activity Instance does not exist- null object reference
Reply: 3

Activity Instance does not exist- null object reference

Chinaedu Onwukwe
Chinaedu Onwukwe Published in 2018-02-12 07:19:20Z

I am trying to develop an Android application. For my use case I want to use a custom typeface and I wrote a that gathers all available TextViews in a View, so that I can set the typeface easily by a loop. I thought I should source out the text manipulation things to an own class named TextManager.class . But now when I am executing the app I am getting an error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

It occurs when I am trying to set Typeface in TextMangaer.class . I did a bit of research and found out that it is because the activity instance does not exist at this point. But I don't get it why, cause when I am trying to do this in Start.class there is no problem.

public class Start extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // set fullscreen

    //Initialize shared preferences
    prefs = getSharedPreferences("User", Context.MODE_PRIVATE);

    TextManager textManager= new TextManager();

    textManager.setTypeface(getTextViews((ViewGroup) findViewById(R.id.root_menu)));


and my TextManager.class:

public class TextManager extends Start{

public TextManager(){

public void setTypeface(List<Integer> idsOfTextViews){
    Typeface typeFaceIkarosLight= Typeface.createFromAsset(getAssets(), "font/ikaros_light.otf");
    for(int i=0; i < idsOfTextViews.size();i++){


So how could I fix this or how should I write this? If anybody could help me figure it out that would be great. Thanks in advance.

Aj 27
Aj 27 Reply to 2018-02-12 07:24:42Z

Its better to make method which return typeface instead passing textview ids as an argument. you can do it like this :

public Typeface getTypeFace(Context context){
    Typeface typeFaceIkarosLight = Typeface.createFromAsset(context.getAssets(), "font/ikaros_light.otf");
    return typeFaceIkarosLight;
Vishal Chhodwani
Vishal Chhodwani Reply to 2018-02-12 07:27:46Z

If you want to use custom fonts then you can take this example

This tutorial will show you how to set custom font on TextView, EditText and on Button.


This will surely help you

Rohit5k2 Reply to 2018-02-12 07:35:17Z

Problem is context is null for getting assets.

Use getContext() or getApplicationContext() in case of being used in an activity but if it is being used in a fragment then use getActivity().getContext()

Typeface font = Typeface.createFromAsset(getContext().getAssets(),  "font/ikaros_light.otf");

Instead of

Typeface typeFaceIkarosLight= Typeface.createFromAsset(getAssets(), "font/ikaros_light.otf");
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312429 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO