Soufiane Rabii
Soufiane Rabii

Reputation: 427

Cast Object to Array - IllegalArgumentException - Argument is not an array

i have an issue with casting and conversion,

messages list is of Object class, each element of the list is an array of different types, so i need a way that will help me cast each element in the object component into array.

This is my portion of code.

    String query = "SELECT m.MSG_CODE, m.MSG_TYPE, m.MSG_TITLE, m.MSG_CORE, me.IS_READ "
            + "FROM EBANKING.EBAN_MESSAGING m, EBANKING.EBAN_MESSAGING_ENDUSER me WHERE me.END_USER_CODE = "
            + userCode + " AND me.MSG_CODE = m.MSG_CODE";
    List<Object> messages = ebankingPersistenceService.getResultNativeQuery(query);
    List<EbanMessagingDto> notifications = new ArrayList<EbanMessagingDto>();

    for (Object e : messages.toArray()) {
        System.out.println(Array.getLength(e)+" ---------------- IS THE LENGTH");
        System.out.println(Array.getLong(e, 0)+" ---------------- IS THE FIRST ELEMENT");
    }

The last line causes always the IllegalArgumentException : Argument is not an array Yet it shows the length as 5 in the previous line.

Any help will be infinitely appreciated.

Thanks in advance.

enter image description here

See guys, e is an array. I have no idea what the going on.

Upvotes: 3

Views: 1471

Answers (4)

rustot
rustot

Reputation: 331

Really this error mean "argument is not array of numbers". Ie if you pass String[] for example:

String[] test = {"123", "124"};
Array.getLong(test, 0);

so, if getLenght(x) returns 5, but getLong(x,0) failed, then x is array, but x[0] can't be converted to long

Upvotes: 1

Ayoub EL
Ayoub EL

Reputation: 56

If you are looking to cast the "e" object to table :

    for (Object e : messages) {
    Object[] data = (Object[]) e;
    System.out.println(Array.getLength(e)+" ---------------- IS THE LENGTH");
    System.out.println(data[0]+" ---------------- IS THE FIRST ELEMENT");
    }

Hope this help.

Upvotes: 3

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

messages.toArray() will give you Object[] and with your loop your traversing the Object[] array with a single object i.e e and later your are passing a single object to Array.getLength(e) and to Array.getLong(e, 0).

Update : the exception is thrown if the value at given index is can't converted to the return type i.e long and in this case it is BidDecimal can't be converted to long so hence exception , so as an alternative you can use get method which will return an Object

Upvotes: 3

Shreyas Sarvothama
Shreyas Sarvothama

Reputation: 485

Array.getLong(e, 0) here it is expecting e to be an array but you are passing an object.. kindly convert it first and then use it...

Another SO link which i found helpful.. its not the same problem as you but something similar.. it might help you and gain more knowledge

Upvotes: -2

Related Questions