In the code below, sBar is an arraylist. I am trying to convert it to String and then write it to file. However, I don’t know what I did wrong here as I keep getting error message saying:
– NullPointerException
– Exception in thread “Thread-1” java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
try{
FileWriter writer = new FileWriter("stime.txt");
for (Iterator it = sBar.iterator(); it.hasNext();) {
String str = (String) it.next();
writer.write(str);
}
}
} catch (IOException e) {
}
You can’t convert a long to a string by casting. Change
to
Another way to write it would be to use a for each loop: