-2

I have below code in my DAO and it is returning a [Ljava.lang.String; cannot be cast to java.lang.String exception I would like to know why this cause and a solution.

public Collection<Testdoc> findTestId(int idTest, String... testtype) {
    StringBuffer buf = new StringBuffer("SELECT w FROM Testdoc w WHERE w.Test.idTest = :idTest");           
    buf.append("AND w.testtype IN ( :testtype ) ");         
    return findByQuery(buf.toString(), "idTest", idTest, "testtype ", testtype );
}
Shadow
  • 33,525
  • 10
  • 51
  • 64
Lilac
  • 580
  • 1
  • 7
  • 25
  • 1
    Why did you declare the second parameter as a `...` varargs parameter, if you don't know what it means/does? *Hint:* It makes the argument into an **array**. `[Ljava.lang.String;` is the internal class name for a `String[]`. – Andreas Jan 24 '20 at 21:35
  • Does this answer your question? [What is the ellipsis (...) for in this method signature?](https://stackoverflow.com/questions/2367398/what-is-the-ellipsis-for-in-this-method-signature) – Kaan Jan 24 '20 at 21:50

1 Answers1

1

The last argument of findByQuery appears to be of type String. You have used a String[].

String... as the last parameter type indicates a String[] but allows the caller to use the likes of fn("x", "y", "z") instead of fn(new String[] { "x", "y", "z" }).

@user85421 points out that findByQuery may well be declared something like:

Collection<Testdoc> findByQuery(String sql, String... args);

In which case you may need to concatenate arguments, though that may still not be correct.

You would then need to concatenate arrays. In the absence of an Arrays.concat, see the question How can I concatenate two arrays in Java?.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305