Java Char Cannot Be Dereferenced

Sheeraz Gul Aug 01, 2022
Java Char Cannot Be Dereferenced

This tutorial demonstrates how to solve Java’s java char cannot be dereferenced error.

Java Char Cannot Be Dereferenced

The error java char cannot be dereferenced occurs when we try to use the equals() method to check that a character is equal to another. In Java, reference is an address to a variable or object, and dereferencing means to access the features of a variable or object through that reference.

Char is a primitive type variable, and dereferencing a primitive type will throw the error char cannot be dereferenced or cannot invoke a method on primitive type char. This error occurs because the primitive is considered raw values instead of objects.

Here is an example that throws the same error:

package delftstack;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Example {
    public static void main(String args[]) {
        do {
            BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter Your String");
            String DemoString = Buffered_Reader.readLine();

            if (DemoString.length() < 10) {
                System.out.println("");
                System.out.println("Please input a valid 10 digit phone number");
                System.out.println("");
            } else {
                if (DemoString.charAt(3).equals('-') && DemoString.charAt(7).equals('-')) {
                    System.out.println("2 Hyphens at 3 and 7");
                } else if (DemoString.charAt(3).equals('-')
                        && DemoString.charAt(8).equals('-')) {
                    System.out.println("2 Hyphens at 3 and 8");
                } else if (DemoString.charAt(3).equals('-')
                        && DemoString.charAt(9).equals('-')) {
                    System.out.println("2 Hyphens at 3 and 9");
                }
            }
        } while (1 < 2);
    }
}

The code above will throw multiple char cannot be dereferenced errors. See output:

Example.java:19: error: char cannot be dereferenced
                if (DemoString.charAt(3).equals('-') && DemoString.charAt(7).equals('-')) {
                                        ^
Example.java:19: error: char cannot be dereferenced
                if (DemoString.charAt(3).equals('-') && DemoString.charAt(7).equals('-')) {
                                                                            ^
Example.java:21: error: char cannot be dereferenced
                } else if (DemoString.charAt(3).equals('-')
                                               ^
Example.java:22: error: char cannot be dereferenced
                        && DemoString.charAt(8).equals('-')) {
                                               ^
Example.java:24: error: char cannot be dereferenced
                } else if (DemoString.charAt(3).equals('-')
                                               ^
Example.java:25: error: char cannot be dereferenced
                        && DemoString.charAt(9).equals('-')) {
                                               ^
6 errors
error: compilation failed

The solution to this error is to use the == operator instead of the equals() method. See the solution:

package delftstack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Example {
    public static void main(String args[]) throws IOException {
        do {
            BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter Your String");
            String DemoString = Buffered_Reader.readLine();

            if (DemoString.length() < 10) {
                System.out.println("");
                System.out.println("Please input a valid 10 digit phone number");
                System.out.println("");
            } else {
                if (DemoString.charAt(3) == '-' && DemoString.charAt(7) == '-') {
                    System.out.println("2 Hyphens at 3 and 7");
                } else if (DemoString.charAt(3) == '-'
                        && DemoString.charAt(8) == '-') {
                    System.out.println("2 Hyphens at 3 and 8");
                } else if (DemoString.charAt(3) == '-'
                        && DemoString.charAt(9) == '-') {
                    System.out.println("2 Hyphens at 3 and 9");
                }
            }

        } while (1 < 2);

    }
}

Now the error char cannot be dereferenced is solved in the code. See output:

Enter Your String0000000000
Enter Your String

Please input a valid 10 digit phone number

Enter Your String
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Error