# Weird Type mismatch when calling value from ArrayList

**URL:** <https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204>\
**Category:** Coding Questions\
**Created:** [July 30, 2018, 1:10pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204 "2018-07-30T13:10:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flashbacker](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@flashbacker](https://discourse.processing.org/u/flashbacker)\
**Post date:** [July 30, 2018, 1:10pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204/1 "2018-07-30T13:10:53Z")

</div>

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 🙂

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()`

```auto
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();
    
  }
}

```

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [July 30, 2018, 1:44pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204/2 "2018-07-30T13:44:06Z")

</div>

Java doesn’t like mixing arrays and collections, according to this [stackoverflow question.](https://stackoverflow.com/questions/8559092/create-an-array-of-arraylists)

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.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [July 30, 2018, 2:15pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204/4 "2018-07-30T14:15:42Z")

</div>

I suspect it may work by declaring messages like this:

```auto
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:

```auto
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:

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 30, 2018, 2:41pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204/5 "2018-07-30T14:41:53Z")

</div>

Use IntList in place of ArrayList\<Integer\>:

- [Processing.org/reference/IntList.html](http://Processing.org/reference/IntList.html)

Use StringList in place of ArrayList\<String\>:

- [Processing.org/reference/StringList.html](http://Processing.org/reference/StringList.html)

---

<div class="post-metadata">

**Author:** ![flashbacker](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@flashbacker](https://discourse.processing.org/u/flashbacker)\
**Post date:** [July 30, 2018, 2:56pm UTC](https://discourse.processing.org/t/weird-type-mismatch-when-calling-value-from-arraylist/2204/6 "2018-07-30T14:56:53Z")

</div>

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
