BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Method Overloading “Reference is Ambiguous” error in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

When working with method overloading, sometimes we encounter compile time error “reference is ambiguous”. This error usually occurs when we pass null value while calling overloaded methods. In this guide, we will see few examples to see when this error occurs and how to avoid it.

Prerequisite: Method Overloading in Java

Method Overloading reference is ambiguous error

Let’s see the following program where this error occurs and then we will discuss why this error occurs and how to avoid it.

public class JavaExample
{
  // Overloaded methods
  public void printParam(Integer i)
  {
    System.out.println("Int: "+i);
  }
  public void printParam(String str)
  {
    System.out.println("String: "+str);
  }

  // Main method
  public static void main(String [] args)
  {
    JavaExample obj = new JavaExample();

    // This will throw compilation error
    obj.printParam(null);
  }
}

Output:
Method Overloading reference is ambiguous Error

In the above example, we have overloaded a method printParam(), one variation of this method accepts Integer values as parameter and other variation accepts String values as parameter.

The reason why we got the error in above program is that the compilation got confused when we passed null value while calling the printParam() method as both Integer and String are not primitive data types, they both accept null values. Compiler is not sure which variation of printParam() method, it should call, thus this error is thrown.

How to avoid error while passing null values to Overloaded methods

Let’s modify the above code like this:

public class JavaExample
{
  // Overloaded methods
  public void printParam(Integer i)
  {
    System.out.println("Int: "+i);
  }
  public void printParam(String str)
  {
    System.out.println("String: "+str);
  }

  // Main method
  public static void main(String [] args)
  {
    JavaExample obj = new JavaExample();

    String s = null;

    // This will not throw an error as it will call String param variation
    obj.printParam(s);
  }
}

Output:

String: null

Here we are not getting any error as we specified the type of null value that we are passing to the printParam() method while calling it. The compiler is not confused in this case as it would call the String param variation of this method.

Another example of Method overloading for null Argument

public class JavaExample
{
  // Overloaded methods
  public void printParam(Integer i)
  {
    System.out.println("Int: "+i);
  }
  public void printParam(String str)
  {
    System.out.println("String: "+str);
  }
  public void printParam(Object obj)
  {
    System.out.println("Object called");
  }
  public void printParam(char[] ch)
  {
    System.out.println("Char Array called");
  }

  // Main method
  public static void main(String [] args)
  {
    JavaExample obj = new JavaExample();

    // This will throw error
    obj.printParam(null);
  }
}

Solution: Lets specify the type of null values while calling the overloaded methods.

public class JavaExample
{
  // Overloaded methods
  public void printParam(Integer i)
  {
    System.out.println("Int: "+i);
  }
  public void printParam(String str)
  {
    System.out.println("String: "+str);
  }
  public void printParam(Object obj)
  {
    System.out.println("Object called");
  }
  public void printParam(char[] ch)
  {
    System.out.println("Char Array called");
  }

  // Main method
  public static void main(String [] args)
  {
    JavaExample obj = new JavaExample();

    //You can call specific overloading method while passing null values
    //like this
    obj.printParam((char[])null);
    obj.printParam((String)null);
    obj.printParam((Integer)null);
    obj.printParam((Object)null);
  }
}

Output:
Fixing null value error in method overloading

Recommended Posts:

  • Method Overloading in Java
  • Difference between Method Overloading and Overriding in Java
  • Method Overriding in Java
  • Java Program to perform Arithmetic operation using Method Overloading
  • Java Program to find area of Geometric figures using Method Overloading
❮ Learn Java

Top Related Articles:

  1. Java Varargs explained with examples
  2. Java Integer byteValue() Method
  3. Constructor Overloading in Java with examples
  4. Java Functional Interfaces
  5. Java – Static Class, Block, Methods and Variables

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap