Array out of bounds exception on simple check box program

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have 4 checkboxes, when selected they should concatenate onto a single string displayed on a Jlabel. It was working before sort-of, not sure whatI did to break it, now I get array exception out of bounds errors. This is the update() method being run whenever a checkbox is interacted with.

http://pastebin.com/tbSpx7jA

It's been answered thanks all, just messed up my initial array declaration.

回答1:

It looks like you're iterating to an index that does not exist:

for (int j = 0; j <= oslist2.length; j++) 

should be

for (int j = 0; j < oslist2.length; j++) 

Java array indexes are (0, 1, 2... length-1)

You also have

oslist2[3]=""; 

which means, you should make the array bigger, or don't use that index. This should work:

String[] oslist2 = new String[4]; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!