4

In my program I am getting a string from Database result set and convert it to char array like this:

emp.nid = rs.getString("nid").toCharArray();

In this part there is no error. The String is successfully converted to char array. But I have another code like this:

nid_txt.setText(emp.nid.toString());

This prints some freaky text. Not the original. Why was this happens? Please help me.

1
  • If you had read the toString() method description you would've figured this out! Commented Sep 2, 2011 at 18:23

5 Answers 5

9

You're calling toString on a char[] - and that inherits the implementation from Object, so you get the char[].class name, @ and then the hash of the object. Instead, call the String(char[]) constructor:

nid_txt.setText(new String(emp.nid));
Sign up to request clarification or add additional context in comments.

Comments

4

This happens because the toString() method is the String representation of the object, and not the String of what it contains.

Try doing like this:

nid_txt.setText(new String(emp.nid));

Comments

2

instead of foo.toString() do new String(foo).

1 Comment

If it's helpful, you should vote it up :-) and accept most helpful answer.
1

You are calling the toString() on the array object. Try:

new String(emp.nid);

and you should see better results.

Comments

1

assuming that emp.nid is byte array second sentence is completely wrong. toString() method in such object won't work. Try insted creating new String based on byte array:

String s = new String(emp.nid);
nid_txt.setText(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.