Tag Archives: Error: char cannot be dereferenced

Error: char cannot be dereferenced [How to Solve]

#Error reported: char cannot be dereferenced

##1. Error code

while(s.charAt(i).equals(" ")){
            i++;
        }

S is a string
## error reason
java: cannot dereference char
char is a basic data type and already a value. Here, the comparison value can be directly used = =.

##Solution 1
but the comparison here is “” (string space)
you can’t directly use the = = symbol
so replace charat() with substring()
substring() returns a string, so you can use equals()

while(s.substring(i,i+1).equals(" ")){
            i++;
        }

So the problem has been solved

##Solution 2
we can also compare it with ” (character space)
you can directly use the equal sign

while(s.charAt(i)==' '){
            i++;
        }