When I try to create the tunnel interface for my VpnService, I'm getting the following error:
Attempt to invoke virtual method 'java.lang.String android.os.ParcelFileDescriptor.toString()' on a null object reference
The only resolve I currently have is to restart the device when this happens. If I do not, I'm unable to create the tunnel at all.
My code for creating the tunnel:
// This is inside my VpnService implementation
private ParcelFileDescriptor configureTunnelWithPushOpts(PushOpts popts)
{
VpnService.Builder builder = this.new Builder();
builder.setMtu ( currentServerPrefs.SERVER_MTU );
builder.addAddress ( popts.ip, 32 );
builder.addDnsServer ( popts.dns1 );
builder.addDnsServer ( popts.dns2 );
builder.addRoute ( "0.0.0.0", 0 );
// Note: Blocking mode is now enabled in native
// code under the setFileDescriptor function.
// builder.setBlocking(true);
builder.setConfigureIntent(
PendingIntent.getActivity(
this,
0,
new Intent(
this,
MainActivity.class
),
PendingIntent.FLAG_UPDATE_CURRENT)
);
final ParcelFileDescriptor vpnInterface;
synchronized (this) {
builder.setSession(currentServerPrefs.SERVER_ADDRESS);
vpnInterface = builder.establish();
}
Logger.info("New interface: " + vpnInterface.toString(), this);
return vpnInterface;
}
Edit:
According to the documentation, the establish
function will return null
if the VpnService has not been prepared, however I am using this to prepare it prior to running the above function.
// This is inside my MainActivity class
Intent intent = VpnService.prepare(getApplicationContext());
if (intent != null) {
startActivityForResult(intent, 0);
} else {
onActivityResult(0, RESULT_OK, null);
}
. . .
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, MyVpnService.class);
startService(intent);
}
}
Phone I'm using for debugging
Google Nexus 5 Running Android 5.0.1
Edit
I figured out how to replicate it so that it's no longer sporadic. Basically, if I uninstall the application while the VPN is connected, and then reinstall it and try to start it back up, I receive a null response when i run
builder.establish()
. I fear this could pose potential issues when the application is updated through the Google Play Store.Aside from that, Please do not mark this question as a duplicate. Every other question on this matter has been given the answer that the service needs to be prepared before the builder is established, however I am preparing it in my case and have a different cause for my problem. Any help would be appreciated.
Thanks a lot for the hint, it brought me to the solution!
I had the same problem in Android 5.0.2: Everything worked fine, and I installed the vpn app I am developing. The next day, suddenly, an unresolvable NullPointerException
, although prepare
was properly called.
The issue could be solved in my environment with the following steps:
null
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With