'Exception occurred while executing 'put'

I'm trying to run an adb shell command programmatically through an app to change the rtt_calling_mode variable value, but I'm running into this error.

 Exception occurred while executing 'put':
 java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:220)
    at com.android.server.appop.AppOpsService.checkPackage(AppOpsService.java:2969)
    at android.app.AppOpsManager.checkPackage(AppOpsManager.java:7697)
    at android.content.ContentProvider.getCallingPackage(ContentProvider.java:954)
    at com.android.providers.settings.SettingsProvider.mutateSecureSetting(SettingsProvider.java:1732)
    at com.android.providers.settings.SettingsProvider.insertSecureSetting(SettingsProvider.java:1652)
    at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:412)
    at android.content.ContentProvider.call(ContentProvider.java:2448)
    at android.content.ContentProvider$Transport.call(ContentProvider.java:517)
    at com.android.providers.settings.SettingsService$MyShellCommand.putForUser(SettingsService.java:375)
    at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:277)
    at android.os.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:98)
    at android.os.ShellCommand.exec(ShellCommand.java:44)
    at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
    at android.os.Binder.shellCommand(Binder.java:929)
    at android.os.Binder.onTransact(Binder.java:813)
    at android.os.Binder.execTransactInternal(Binder.java:1159)
    at android.os.Binder.execTransact(Binder.java:1123)

When running on A10 I was able to run this command perfectly fine and It would change the RTT variable as if I ran the command 'adb shell settings put secure rtt_calling_mode0 1' via the command prompt. But now on an A11 build I receive this error. I'm not sure what could be causing this issue and why it now has an issue with 'put' now. Also not sure where the NullPointerException is coming from either. Does anyone know what could be causing this issue? Below is the code that's throwing the error when its run.

public static String setRealTimeText(int simSlot, int enabled){
        String command = "settings put secure rtt_calling_mode" + slot +  " " + enabled;

        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader errorOutput = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            //get the error output
            String s = "";
            String outputString = "";

            if (errorOutput.readLine() != null) {
                while ((s = errorOutput.readLine()) != null) {
                    outputString += " " + s + "\n";
                }
                return outputString;
            }

            //get the console output
            while ((s = output.readLine()) != null) {
                outputString += "- " + s + "\n";
            }
            return "RTT was enabled"

        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
    }


Solution 1:[1]

If you do not need to issue a shell command, you can programmatically do

Settings.Secure.putInt(getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 1);

But you need root access to do this. Only system apps have permission to change secure settings. This might as well be the cause for the exception when running the shell command.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1