Search code examples
androidviewmodelruntimeexceptionillegalaccessexception

ViewModel class constantly returns Runtime and IllegalAccessException


I posted this question yesterday but I think that I might not have been very clear with the doubt.

In the line where I use ViewModel to link up with MainActivity using the ViewProvider, the code returns the Runtime Exception. Additionally, the code also returns the message "Cannot create an instance of class at com.example.viewmodelpractice.ViewModelStorage".

The error message points specifically at this line:

viewModelStorage = new ViewModelProvider(this).get(ViewModelStorage.class);

Here are the rest of the classes:

ViewModelStorage.java

package com.example.viewmodelpractice;

import androidx.lifecycle.ViewModel;

class ViewModelStorage extends ViewModel {

private String name;
private String age;

String getName() {
    return name;
}

void setName(String name){
    this.name = name;
}

String getAge() {
    return age;
}

void setAge(String age){
    this.age = age;
}
}

Main Activity.java

package com.example.viewmodelpractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText nameEditText;
    private EditText ageEditText;
    private TextView resultTextView;

    private ViewModelStorage viewModelStorage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameEditText = findViewById(R.id.name_notice_editText);
        ageEditText = findViewById(R.id.age_notice_editText);
        resultTextView = findViewById(R.id.result_textView);

        viewModelStorage = new ViewModelProvider(this).get(ViewModelStorage.class);
        storeDataMethod();
    }

    private void storeDataMethod() {
        viewModelStorage.setName(nameEditText.getText().toString());
        viewModelStorage.setAge(ageEditText.getText().toString());
        getDataMethod();
    }

    private void getDataMethod() {
        resultTextView.setText(("Hey there, ").concat(viewModelStorage.getName().concat(". You are ").concat(viewModelStorage.getAge()).concat(" years old.")));
    }
}

Error Message

2020-05-04 12:06:38.744 20688-20688/com.example.viewmodelpractice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.viewmodelpractice, PID: 20688
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewmodelpractice/com.example.viewmodelpractice.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.viewmodelpractice.ViewModelStorage
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.viewmodelpractice.ViewModelStorage
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:223)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at com.example.viewmodelpractice.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6823) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

Caused by: java.lang.IllegalAccessException: java.lang.Class<com.example.viewmodelpractice.ViewModelStorage> is not accessible from java.lang.Class<androidx.lifecycle.ViewModelProvider$NewInstanceFactory>
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
        at com.example.viewmodelpractice.MainActivity.onCreate(MainActivity.java:27) 
        at android.app.Activity.performCreate(Activity.java:7224) 
        at android.app.Activity.performCreate(Activity.java:7213) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6823) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Solution

  • Instead of this:

    class ViewModelStorage extends ViewModel {
    

    Make it like this:

    public class ViewModelStorage extends ViewModel {
    

    Due to this, you are getting the IllegalAccessException!