Can't cast config.getStringList().toArray() as String[]

Discussion in 'Plugin Development' started by oxguy3, Sep 28, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    oxguy3

    Okay, so my plugin CoolStoryBro randomly assigns people "bro names" from a giant list. I'm trying to put that list in my config file, but having trouble with object types. Here's my troubled code (CC BY-SA, jsyk): http://pastebin.com/PjSHDzhF

    Here is the log of the error I'm getting, if you want to see it: http://pastebin.com/5iHGRunj

    And lastly, here is what I got when I logged bronameslist.toString() right after the line "bronameslist = config.getStringList("bro_names", null);" As you can see, every object is a String: http://pastebin.com/RwmTxvhp


    Can someone tell me how to get around this ClassCastException? Do I have to loop through the List and cast each individual element as a String?

    Thanks!
     
  2. Offline

    xpansive

    You shouldn't have to cast it at all. I think.
     
  3. Offline

    oxguy3

    Nope, Eclipse gives an error if I don't do something about it. The first (and only useful) suggestion it give to fix it is casting as String[].
     
  4. .toArray returns Object[] , but there is parameter. Example to get a String[]:
    String[] myarray = mylist.toArray(new String[mylist.size()]);
    Cant explain much as im on my iphone but hopefully that makes sense
     
  5. Offline

    oxguy3

    I saw that earlier looking through the Arrays Javadocs, but completely forgot about it. Seems to have worked perfectly, thanks tips48!
     
  6. Any time :). I only know because I had a similar problem :p
     
  7. Offline

    oxguy3

    Lol, it definitely worked, thanks so much! CoolStoryBro v0.3 is going up right now. :D
     
    tips48 likes this.
Thread Status:
Not open for further replies.

Share This Page