Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm probably being very silly, but it's driving me nuts. I have searched around but am relatively new to programming and am in over my head. Please help if you can! the bar parameter is taking an arraylist.toArray() and it's just full of strings.

public bar(Object[] contents) {

    for (Object o : contents) {
        String s = (String) o;
        if (s == null) {
            System.out.println("newline"); //checking
        } else  {
            try {
                arrayContents.add(new line(s));

            } catch (Exception ex) {
                System.out.println("error printing note: " + s + " " + ex + " in bar " + i);
            }
        }
        i++;

    }
//        System.out.println(arrayContents);
}
share|improve this question
    
Is it an ArrayList<String>? Also, post your full stack trace. –  Sotirios Delimanolis Oct 30 '13 at 21:29
    
It wasn't but the below answer is perfect. I will post stack trace in future –  juju Oct 31 '13 at 0:56

1 Answer 1

up vote 6 down vote accepted

Do this instead

    String s = String.valueOf(o);

You cannot cast an Object to String if it is not a String object. You'll need to invoke toString method on the object instead - unless the object is null. String.valueOf() takes care of that

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}
share|improve this answer
    
thanks man. That's perfect. –  juju Oct 31 '13 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.