Java char cannot be dereferenced error

need help getting rid of this error in a simple text editor program

Page 1 of 1

1 Replies - 1978 Views - Last Post: 29 April 2009 - 05:39 PM Rate Topic: -----

#1 chipmunk5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 09

Java char cannot be dereferenced error

Posted 29 April 2009 - 02:19 PM

I'm working on an assignment that requires me to write a simple text editor program that will find a string in a text file. i have it working without this feature, but when i added it i get a compile error. here are the lines that are giving me trouble. I'd appreciate anyone's help on this.

I'm trying to get the characters before the found word and after the found word and store them in the variables a and o. If neither of these are letters, there is a match and i need to return i.

if (j == key.length())
{
	if (!whole_words)	// match anything
		return i;
	else				// match whole words only
	{
		char a = null;
		char o = null;

		if (i > 0)
			a = text.charAt(i - 1);
		if (i + key.length() > text.length())
			o = text.charAt(i + key.length());

		if (a == null && o == null)	// whole line match
			return i;
		else if (a == null && !o.isLetter())  // first word match
			return i;
		else if (!a.isLetter() && o == null)  // last word match
			return i;
		else if (!a.isLetter() && !o.isLetter())	// inner word match
			return i;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java char cannot be dereferenced error

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java char cannot be dereferenced error

Posted 29 April 2009 - 05:39 PM

null applies to objects only char is a basic data type it cannot be null
it can be 0 but it can't be null
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1