Pages

Monday, December 13, 2021

Java Insert Dimensions To Complete Referencetype [Fixed]

1. Overview

In this tutorial, We'll learn how to fix the common compile time error "Syntax error, insert "Dimensions" to complete ReferenceType" in java.

This error occurs when you are working with the java generic types.

It is suggested to follow the generic naming conventions and rules with collection api.

Compile time error

Syntax error, insert "Dimensions" to complete ReferenceType.

At the end of the article, we've given GitHub link for the examples shown in this post.

Java Insert Dimensions To Complete Referencetype [Fixed]


2. Java Insert Dimensions To Complete Referencetype Example and Fix

A simple example on insert dimensions to complete reference type and how to simulate this error.

A note is to remember that you are working with the raw generic types along with the wrapper objects and primitive values.

Example 1

package com.javaprogramto.collections.generics;

import java.util.ArrayList;
import java.util.List;

public class GenericsCompileTimeError {

	public static void main(String[] args) {
		
		List<Integer> integers = null;
		
		integers = new ArrayList<int>();
	}
}

Compile time error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert "Dimensions" to complete ReferenceType

	at com.javaprogramto.collections.generics.GenericsCompileTimeError.main(GenericsCompileTimeError.java:12)

Here, List reference is declared as Integer wrapper type but when initializing ArrayList, we have passed the int which is a primitive data type.

So, here data type mismatch between the generic declaration and initialization.

To fix this syntax error, we need to replace the primitive int with Wrapper class Integer.

Example to fix the above syntax error

import java.util.ArrayList;
import java.util.List;

public class GenericsCompileTimeErrorFix {

	public static void main(String[] args) {

		List<Integer> integers = null;

		integers = new ArrayList<Integer>();

		integers.add(100);
	}
}

There is no compile time error now. If you are using eclipse, you do see the instant result because eclipse does the auto compile.

We've shown in the declaration but this can be seen in the method type argument.


3. Another Example on Insert Dimensions To Complete Referencetype and Fix

Example 2

package com.javaprogramto.collections.generics;

import java.util.Map;
import java.util.TreeMap;

import com.javaprogramto.models.Employee;

public class GenericsCompileTimeError2 {

	public static void main(String[] args) {
		
		Map<Employee, Boolean> integers = null;
		
		integers = new TreeMap<Employee, boolean>();
	}
}

Error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert "Dimensions" to complete ReferenceType

	at com.javaprogramto.collections.generics.GenericsCompileTimeError2.main(GenericsCompileTimeError2.java:14)


Fix

To fix this error, you need to replace boolean with Boolean wrapper class as below.

package com.javaprogramto.collections.generics;

import java.util.Map;
import java.util.TreeMap;

import com.javaprogramto.models.Employee;

public class GenericsCompileTimeError2Fix {

	public static void main(String[] args) {
		
		Map<Employee, Boolean> emps = null;
		
		integers = new TreeMap<Employee, Boolean>();
	}
}

This code produces no compile-time errors.


4. Conclusion

In this article, we've seen how to fix generic Syntax errors, insert "Dimensions" to complete ReferenceType in java with examples.

To fix the error, we have to use the wrapper class in place of primitive type in collection classes declarations and initialization.

GitHub

Java Generics

Java type inference in Generics


No comments:

Post a Comment

Please do not add any spam links in the comments section.