3

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.

1
  • Can you show us the output as well please? Commented Oct 19, 2016 at 15:39

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help, i was totally misguided.
3

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

4 Comments

@SotiriosDelimanolis yeah Mr.Delimanolis but there can be lot of possibilities but as you can see the full code is not there about what this ebankingPersistenceService.getResultNativeQuery(query) returns but if you connect the dots , the two function with Array class need an array but they didn't get hence exception
the ebankingPersistenceService.getResultNativeQuery(query) returns a List<Object>.
@SoufianeRabii the Object can hold anything and in this case the Array class is unable to convert your e input to long so you can use the alternative i.e get function
@SotiriosDelimanolis yup that was an array and it is clear now due to the error pic , the object was bigDecimal which can't be convert to long so i suggested the alternative solution , thanks for your time
1

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

Comments

-2

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

4 Comments

How do you explain that getLong throws an exception, but getLength does not, for the same argument?
Why is it showing the length then if it's not an Array
@SoufianeRabii kindly paste the complete code and the output for the same.. you posting the output will help us a lot in solving this issue
@SotiriosDelimanolis then what is your solution for this? I will answer to your question once Soufiane posts her output as well

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.