Java – WorkManager could not find the required zero-parameter constructor

WorkManager could not find the required zero-parameter constructor… here is a solution to the problem.

WorkManager could not find the required zero-parameter constructor

I’m using WorkManager 1.0.0-alpha02 found in the android.arch.work:work-runtime dependency to query a web page once per minute (the query code is excluded from this example because it’s not relevant).

When I use WorkManager like this:

WorkManager
                .getInstance()
                .enqueue(
                        new PeriodicWorkRequest.Builder(
                                MessageWorker.class,
                                1,
                                TimeUnit.MINUTES
                        )
                                .setConstraints(
                                        new Constraints.Builder()
                                                .setRequiredNetworkType(NetworkType.CONNECTED)
                                                .build()
                                )
                                .build()
                );

private class MessageWorker extends Worker {
        public MessageWorker() {
        }

@NonNull
        @Override
        public WorkerResult doWork() {
             Do stuff
            return WorkerResult.SUCCESS;
        }
}

It gives this error message:

05-31 17:50:05.645 11749-12168/com.neelkamath.webview E/WorkerWrapper: Trouble instantiating com.neelkamath.webview.MainActivity$MessageWorker
    java.lang.InstantiationException: java.lang.Class<com.neelkamath.webview.MainActivity$MessageWorker> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at androidx.work.impl.WorkerWrapper.workerFromClassName(WorkerWrapper.java:405)
        at androidx.work.impl.WorkerWrapper.workerFromWorkSpec(WorkerWrapper.java:377)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:132)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
05-31 17:50:05.645 11749-12168/com.neelkamath.webview E/WorkerWrapper: Could for create Worker com.neelkamath.webview.MainActivity$MessageWorker

I’ve tried the following constructors to no avail:

  • There is no constructor
  • Public
  • Private
  • A constructor with a non-empty body, the only statement is super();

Solution

Remove the following constructor from the MessageWorker and make the class public. :

public MessageWorker() {
}

Also make sure that the class is in a separate file.

Related Problems and Solutions