Java: ClassCastException - [Ljava.lang.Long; cannot be added to java.lang.Long

I use red5 and set / get attributes using the IConnection class, but this is really not relevant.

'L' means long in java. therefore, 0L is type 0 Long, not just '0', which is 0 type Integer.

What is the difference between [Ljava.lang.Long and java.lang.Long in the following error message:

 stack trace: java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.lang.Long 

Update

code example:

  static Long getLongAttribute(IConnection conn, String attribute) { Long result=null; try { if (!conn.hasAttribute(attribute)) throw new Exception(attribute + " - Long attribute not found!"); result = conn.getLongAttribute(attribute); // <--- ERRROR ON THIS LINE } catch (Exception e) { _handleException(e); } return result; } 
+4
source share
4 answers

The first object is a Long array, the second is only Long . try it

  Long l = 1l; Long[] l2 = {}; System.out.println(l.getClass()); System.out.println(l2.getClass()); 

Output

 class java.lang.Long class [Ljava.lang.Long; 

But I agree that the view is [L_class_; for array types is very confusing. I wonder how it happened?

+21
source

Your code is trying to use Long[] to Long , which throws a ClassCastException

+2
source

[Ljava.lang.Long is a list of java.lang.Long s

EDIT: as indicated below, an array. Sorry, I typed too fast ...

+1
source

I had a similar problem where my code was

List<Object[]> rows = criteria.list();

But my criteria have only a projection count (*) and therefore criteria.list() returns only List<Long> instead of List<Long[]>

I resolved it by changing it to

List<Object> rows = criteria.list();

+1
source

Source: https://habr.com/ru/post/1333045/


All Articles