Weird Type mismatch when calling value from ArrayList

Hello,

in a project, i’m creating an array of arrayLists that i’m filling with integers in a function and then sending through OSC messages.
But if I try to add one of the value to the osc message:
myMessage.add(messages[i].get(j));

it returns the error:
The method add(Object) from the type OscMessage is not visible

if I try to break it down: int val = messages[i].get(j); I get:
Type mismatch, "java.lang.Object" does not match with "int"

On the other hand, if I test it with instanceof I find out it’s an integer as expected.
I’m printing the Arraylist in several places and it’s showing expected values…
I’ve tried also to force the object as an integer with int(messages[i].get(j)); but it doesn’t work as well…
I’m totally out of clues… Any hints or similar experience would be appreciated :slight_smile:

Here is my full code in case it helps: I’m filling the ArrayLists in synchronized void portraitCourt() which is called once when synchronized void oscEvent() receives a message. Osc message is built and sent in synchronized void sendPlaylis()

import netP5.*;
import oscP5.*;

OscP5 oscP5;
NetAddress screen[];  // RPI receiver OSC object
ArrayList[] messages; 

String playlist[];           // to load the videos list
String file[];     
String RPIs[];             //RPI video players network configs

int type;
int perso;
int intention;
int number;

int endCount = 0; //how many RPIs finished their sequence

void setup(){
  playlist = loadStrings("playlist.txt");
  RPIs = loadStrings("RPIs.txt");
  screen = new NetAddress[RPIs.length];
  messages = new ArrayList[RPIs.length];
  for(int i=0; i<RPIs.length; i++){
    messages[i] = new ArrayList<Integer>();
  }
  oscP5 = new OscP5(this,8000);
  for(int i=0; i<RPIs.length; i++){
    String[] infos = split(RPIs[i],"*");
    screen[i] = new NetAddress(infos[0],int(infos[1])); 
  }
}

void draw(){
  if(endCount == RPIs.length){
    println("everybody's done!");
  }
}


/// send all the messages when playlists are generated
synchronized void sendPlaylist(){
 for(int i=0; i<RPIs.length; i++){
   OscMessage select = new OscMessage("/select");
   if(messages[i].get(0) instanceof Integer){
     println("it's an integer");
   }else{println("it's not!!");}
   for(int j=0; j<messages[i].size(); j++){
     println("all values are    " + messages[i].get(j));
     //int val = messages[i].get(j);   // Doesn't work
     //select.add(messages[i].get(j));     // Doesn't work
   //oscP5.send(select,screen[i]);
 }
}

synchronized void portraitCourt(String perso, int nbPortrait, int screenNum){
  ArrayList<String> portrait = new ArrayList<String>();
  for(int i=0; i<playlist.length; i++){
    String[] video = split(playlist[i],"_");
    if(video[1].equals(perso)==true && video[0].equals("E")==true){ 
    println("found match");
    portrait.add(i + "*" + playlist[i]);    // adds selected videos in arraylist (index*name)
    }
  }
  println("portrait    " + portrait);
   messages[screenNum].clear();
  for(int i=0; i<nbPortrait; i++){
    int r = int(random(0, portrait.size()-1));
    String[] rank = split(portrait.get(r),"*"); // can be skipped if video name removed from arraylist
    messages[screenNum].add(int(rank[0]));
  }
  println("message is   " + messages[screenNum] + "     stop");
    
}


synchronized void oscEvent(OscMessage theOscMessage){
  if(theOscMessage.checkAddrPattern("/end")==true){
    endCount =+ 1;
  }
  
  if(theOscMessage.checkAddrPattern("/play")==true){
    println("message received");
    portraitCourt("2",3,0);
    sendPlaylist();
    
  }
}

Java doesn’t like mixing arrays and collections, according to this stackoverflow question.

Perhaps try a List of ArrayLists?

On the other hand, does your use case require the flexibility of an ArrayList? If you know how large their going to be, you could possibly try a two-dimensional int-array.

I suspect it may work by declaring messages like this:

ArrayList<Integer>[] messages;

Here a small test program that works by adding the Integer type and does not compile in the Processing IDE without it:

ArrayList<Integer>[] messages; 

messages = new ArrayList[3];
for (int i=0; i<3; i++) {
  messages[i] = new ArrayList<Integer>();
  int c = (int)random(3, 6);
  for(int j=0; j<c; j++) {
    messages[i].add((int)random(100, 200));
  }
  println(messages[i]);
}

println(messages[0].get(2) * 5);

Explanation: when declaring a generic array of ArrayList, the declaration does not contain information about what type of ArrayList it will hold, so it defaults to ArrayList<Object>.

You can later put ArrayList<Integer>'s into the array, because an Integer is also an Object. Later, if you read out an item from one of the ArrayList and try to do something to that item that requires it to be an integer, it does not work, because the item could actually be something else, for instance, a item from an ArrayList<PVector>. Unless you specify Integer when declaring the array of ArrayList. Then the array can only hold ArrayList<Integer>'s, and is safe to assume that the items in the ArrayList are Integers.

This example shows why it can not assume the content is an Integer:

ArrayList[] messages; 

messages = new ArrayList[2];
messages[0] = new ArrayList<Integer>();
messages[1] = new ArrayList<PVector>();

messages[0].add(2);
messages[0].add(27);
messages[1].add(new PVector(19,84));

println(messages);

Use IntList in place of ArrayList<Integer>:

Use StringList in place of ArrayList<String>:

Thanks all for these suggestions!

@hamoid technic did the trick by declaring ArrayList<Integer>[] messages;
It works now.

I’ll check other solutions to see what’s best. thanks again