Guy Rich
Guy Rich

Reputation: 451

Why am I getting an incompatible types from the java compiler?

I'm a Java newbie, but I am an experienced programmer (Assembler, COBOL, C/C++, REXX)

I can't understand why the compiler is giving me the following:

[loading java\lang\Throwable.class(java\lang:Throwable.class)]
src\LU62XnsCvr.java:265: incompatible types
found   : java.lang.String
required: java.lang.StringBuffer

Code:

message_data = "LU62XCE0513: Request File I/O Exception =" ;

I've defined message_data as:

static StringBuffer message_data = new StringBuffer();

So why can't I place a simple literal String into the StringBuffer labeled message_data ? I mean BOTH are of type String .. right ??

Upvotes: 2

Views: 2402

Answers (3)

Val
Val

Reputation: 173

Just call message_data.toString() when passing the message to the Throwable

Upvotes: -2

Howard
Howard

Reputation: 39197

Both objects may represent a string but they are thechnically not both of type string. You can create a stringbuffer from a string directly via new StringBuffer(message_data).

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

No, String and StringBuffer are totally unrelated. Try

message_data.append("LU62XCE0513: Request File I/O Exception =");

While we're at it: note that StringBuffer is a legacy class, best replaced with StringBuilder in new code, except in the unlikely case that you need to share the object across threads. It's source compatible, so just change the name and you're fine.

Upvotes: 7

Related Questions