Tutorial

"The method X is ambiguous for the type Y" Java ambiguous method call null error

Published on August 3, 2022
Default avatar

By Pankaj

"The method X is ambiguous for the type Y" Java ambiguous method call null error

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

If you are reading this, chances are you got The method X is ambiguous for the type Y error when compiling a java program in terminal or in any Java IDE.

Java ambiguous method call

Here I am going to explain why java ambiguous method call error comes with some examples. This ambiguous method call error always comes with method overloading where compiler fails to find out which of the overloaded method should be used. Suppose we have a java program like below.

package com.journaldev.errors;

public class Test {

	public void foo(Object o) {
		System.out.println("Object");
	}

	public void foo(String s) {
		System.out.println("String");
	}
	public static void main(String[] args) {
		new Test().foo(null);
	}

}

Above program compiles perfectly and when we run it, it prints “String”. So the method foo(String s) was called by the program. The reason behind this is java compiler tries to find out the method with most specific input parameters to invoke a method. We know that Object is the parent class of String, so the choice was easy. Here is the excerpt from Java Language Specification.

If more than one member method is both accessible and applicable to a method invocation … The Java programming language uses the rule that the most specific method is chosen.

The reason I am passing “null” is because it works for any type of arguments, if we pass any other objects the choice of method for the java compiler is easy.

The method X is ambiguous for the type Y

Now let’s add below method to the above code.

public void foo(Integer i){
	System.out.println("Integer");
}

You will get compile time error as The method foo(Object) is ambiguous for the type Test because both String and Integer class have Object as parent class and there is no inheritance. So java compiler doesn’t consider any of them to be more specific, hence the method ambiguous call error.

package com.journaldev.strings;

public class Test {

	public void foo(Object o) {
		System.out.println("Object");
	}

	public void foo(Exception e) {
		System.out.println("Exception");
	}

	public void foo(NullPointerException ne) {
		System.out.println("NullPointerException");
	}

	public static void main(String[] args) {
		new Test().foo(null);
	}

}

As above explained, here foo(NullPointerException ne) is the most specific method because it’s inherited from Exception class and hence this code compiles fine and when executed prints “NullPointerException”. I hope this article clarifies any doubt you have with java ambiguous method call compiler error, please comment if you want to add something or if you have some confusion with this.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 28, 2020

public class PolyDemo3 { public static void main(String[] args) { PolyDemo3 p1=new PolyDemo3(); //p1.funA(); A x=new A(); p1.funA(x); p1.funA(new A()); p1.funA(null); A y=new A(); p1.funA(y); p1.funA(new A()); A z=new A(); p1.funA(z); p1.funA(new A()); } void funA() { System.out.println(“funA of PolyDemo3”); } void funA(A a1) { System.out.println(“classA of PolyDemo3”); System.out.println(a1); } void funA(B a1) { System.out.println(“classB of PolyDemo3”); System.out.println(a1); } void funA(C a1) { System.out.println(“classC of PolyDemo3”); System.out.println(a1); } } how to call the null address in class A and B and C

- chaitanya

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 16, 2019

    Hi All, I am using java 8 and in my system haven’t any warning regarding B b=new B(); new B().foo(null); And because of public void foo(String ne) { System.out.println(“String”); } overloaded method java display ambiguous error. eighter use public void foo(Exception e) { System.out.println(“Exception”); } public void foo(NullPointerException ne) { System.out.println(“NullPointerException”); } Or use public void foo(String ne) { System.out.println(“String”); }

    - Umesh Soni

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      January 16, 2018

      Good explanation on ambiguity in method overloading. https://www.youtube.com/watch?v=xlSRf7psJHE

      - Kavita

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        July 4, 2017

        Class Met { public void m(int a,float b) { System.out.println(“first m”); } Public void m(float a,char b) { System.out.println(“sec m”); } } Class Main1 { Public static void main (string arg[]) { Met n= new Met(); n.m( ‘c’,‘c’); } Output is ambiguous ,why it is ambiguous and can I know the reason and procedure for checking it Thanks

        - Ravi teja

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 15, 2016

          What will be the out put for below program ? class B{ public void foo(Object o) { System.out.println(“Object”); } public void foo(Exception e) { System.out.println(“Exception”); } public void foo(NullPointerException ne) { System.out.println(“NullPointerException”); } public void foo(String ne) { System.out.println(“String”); } } class A extends B { public static void main(String[] args) { B b=new B(); new B().foo(null); } } Could you please explain ? Thanks, Bhargav

          - Bhargav

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 22, 2016

            Thank you so much best example with best explanation.

            - shef

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              December 29, 2015

              Thanks for your explanation! How can I avoid this error or change it to warning?

              - Mike Zang

                Try DigitalOcean for free

                Click below to sign up and get $200 of credit to try our products over 60 days!

                Sign up

                Join the Tech Talk
                Success! Thank you! Please check your email for further details.

                Please complete your information!

                Get our biweekly newsletter

                Sign up for Infrastructure as a Newsletter.

                Hollie's Hub for Good

                Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                Become a contributor

                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                Welcome to the developer cloud

                DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                Learn more
                DigitalOcean Cloud Control Panel