2

I am creating common java class for ProgressBar i getting error like below

java.lang.NullPointerException: Attempt to invoke virtual method 'void app.bridgecenterandstaffmanagament.com.bridgecenterandstaffmanagament.CommonClass.Singletonclass_obj.showProgressBar(android.content.Context)' on a null object reference
                                                                                                                           at app.bridgecenterandstaffmanagament.com.bridgecenterandstaffmanagament.Fragments.NotificationFragment.onCreateView(NotificationFragment.java:41)

Fragment

public class NotificationFragment extends Fragment {


    public NotificationFragment() {
        // Required empty public constructor
    }

    RecyclerView recyclerView;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.fragment_notification, container, false);


        recyclerView = v.findViewById(R.id.recylierview_notification);



       // Singletonclass_obj.getInstance().showProgressBar(getActivity());
        Singletonclass_obj.getInstance().showProgressBar(getContext());
       // print();

        return v;
    }


}

Java class

public class Singletonclass_obj extends Application{
    private  ProgressBar progressBar;
    private static Singletonclass_obj singletonclassobj;



    public static Singletonclass_obj getInstance() {
        return singletonclassobj;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        singletonclassobj = this;
    }

    public void showProgressBar(Context context)
    {
      //  Toast.makeText(context, "welcometoindia", Toast.LENGTH_SHORT).show();
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
    }

    public void hideProgressBar()
    {
        if (progressBar !=null && progressBar.isShown())
            progressBar.setVisibility(View.INVISIBLE);
     }

}

Same codeing working fine my last Project..please help me any one.

demo
  • 672
  • 3
  • 9
  • 34

2 Answers2

4

as per my above comment in question

try this add your Singletonclass_obj in to manifest file inside application tag like below code

<application
        android:name=".Singletonclass_obj"<!--add here your Singletonclass_obj class -->
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
3

The problem was that you didn't initialise correctly the application class. Basically, the system doesn't know about your class and hence it is not created. You need to add the following in you <application> tag from Manifest and it will work:

android:name=".Singletonclass_obj" 

and call it like this: ((Singletonclass_obj)getActivity().getApplication()).showProgressBar(getContext());

However, as I said in comments, you don't need to create the application class as a singleton because it already is. So your application class would be something like this:

public class Singletonclass_obj extends Application {
    private ProgressBar progressBar;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void showProgressBar(Context context)
    {
//          Toast.makeText(context, "welcometoindia", Toast.LENGTH_SHORT).show();
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
    }

    public void hideProgressBar()
    {
        if (progressBar !=null && progressBar.isShown())
            progressBar.setVisibility(View.INVISIBLE);
    }

}

Even if this will fix your issue regarding the NPE, you won't be able to use the ProgressBar because you don't have a view to add it (the application doesn't have a view).

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31