While running a Java program, are you getting “error: char cannot be dereferenced” compilation error in calling methods like equals, isLetter, etc? In this post, we’ll look at the reasons you are getting this error and the solutions to fix it.
Why do we get this error?
Char cannot be dereferenced – compilation error occurs when you call a method on a primitive variable of type char. A char variable only holds a character value, and it does not contain any methods like that of an object. Because of this, Java raises a compilation error if you try to call a method on a primitive variable.
What does the meaning of “char cannot be dereferenced”?
When we call a method on a reference variable, it gets dereferenced to the object. Since we are calling a method on a primitive variable, Java cannot dereference it to any object, as there won’t be any object as such for a primitive variable.
For example, you can store “Hello World” in a variable declared using String class. In this case, the variable is not a primitive variable it is a reference variable of an object of type String class. String is a class, not a primitive data type. A class contains variables and methods within itself. Its instance (object) can call those methods available within that class to perform an operation on the value. However, a primitive data type (char, int, float, long, double etc) do not have any attributes and methods within itself, so you won’t be able to call the way you do it for objects.
Sometimes, you may call a String class methods such as charAt, toUpperCase, toString, or compareTo etc., from the return type of another method which actually return a char. In this case, you would definitely get “char cannot be dereferenced” compilation error.
Code Examples
Problem Code Example 1
package com.techstackjournal;
public class JavaDemo1 {
public static void main(String[] args) {
char cvar = 'X';
if (cvar.equals("X")) {
System.out.println("c is X");
} else {
System.out.println("c is not X");
}
}
}
In this example, we declared a char variable cvar and initialized it with value ‘X’, notice that X is within single quotations and not double quotations. If you include a character within single quotations, java recognizes it to be a char type of value, if you include it within double quotations Java will treat it as a String.
Then, we are trying to compare whether the value within variable cvar is equals to ‘X’ by calling equals method on top of variable cvar. You need to pay attention here that this variable is a primitive variable, and it doesn’t have any attributes or methods of its own. So in short, you won’t be able to call the equals method on cvar.
So, check your code, if you are calling the method (which is printed on the error) on top of a primitive variable, you need to correct it by directly using the operators instead of method calls.
Example 1 Code Fix
Since, we just want to compare the values of the variable and value ‘X’, you can use double equal here and make sure to enclose X within single quotations, as you are comparing it against a char variable.
package com.techstackjournal;
public class JavaDemo1 {
public static void main(String[] args) {
char c = 'X';
if (c == 'X') {
System.out.println("c is X");
} else {
System.out.println("c is not X");
}
}
}
Problem Code Example 2
package com.techstackjournal;
public class JavaDemo2 {
public static void main(String[] args) {
String str = "This is a sample program";
char cvar;
int cnt = 0, i = 0;
do {
if (str.charAt(i).equals(" ")) {
cnt++;
}
i++;
} while (i < str.length());
System.out.println("Number of spaces = " + cnt);
}
}
In this example, we are trying to count the number of spaces. For that, we are reading each character individually using charAt() function and checking if it is space or not. The charAt() function of String class returns a char, but we are attempting to call equals method on top of charAt method which is not possible as it returns char and you cannot call equals method on char variables.
Example 2 Code Fix
package com.techstackjournal;
public class JavaDemo2 {
public static void main(String[] args) {
String str = "This is a sample program";
char cvar;
int cnt = 0, i = 0;
do {
if (str.charAt(i) == ' ') {
cnt++;
}
i++;
} while (i < str.length());
System.out.println("Number of spaces = " + cnt);
}
}
To fix the issue, we again replacing the equals method call with the == operator.