Home » Android » java – Passing null in putExtra in Android Intent giving compile time error?

java – Passing null in putExtra in Android Intent giving compile time error?

Posted by: admin June 15, 2020 Leave a comment


I’m trying to use putExtra(String,String) in my code to pass null.
As the parameter suggests the second parameter can be null as it is a string and I can send it

this.getIntent().putExtra(AppConstant.TestString, null);

When i use the above code it gives me error saying :

The method putExtra(String, String) is ambiguous for the type Intent

However it allows me to use :

this.getIntent().putExtra(AppConstant.TestString, "");

Kindly enlighten me on this. Thanks in advance.

How to&Answers:

When you use null, the compiler doesn’t know which is the type you want to use and cannot decide which overload of the method to use.

You can cast your null to String to inform compiler which method you use:

this.getIntent().putExtra(AppConstant.TestString, (String)null);

Alternatively, you can create a variable and set it to null:

String param = null;
this.getIntent().putExtra(AppConstant.TestString, param);


The problem is as the second parameter is null it can’t tell which method you want to call: putExtra(String name, float value), putExtra (String name, byte[] value), putExtra (String name, long[] value), etc…


This is probably because Android can not determine what you are passing (String,boolean,int) when you use

this.getIntent().putExtra(AppConstant.TestString, null);

but when you use

this.getIntent().putExtra(AppConstant.TestString, "");

it knows that you are passing it as a String


Use intent.removeExtra(extraName) instead. It clears the extra and is more clean than using typecasting in putExtra.