Good Day I have this error in my Java code. I'm trying to link my database to azure cloud. I used list commands:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at anothercloud.Anothercloud.main(Anothercloud.java:63)
Java Result: 1
package anothercloud;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
*
* @author ariel
*/
public class Anothercloud {
static HashMap<Double, String> redisData = new HashMap<Double, String>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean useSsl=true;
inclass infos=new inclass();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> value = new ArrayList<String>();
String anjiecachekey="xxxx=";
JedisShardInfo shardInfo=new JedisShardInfo("anjiecloud.redis.cache.windows.net",6380,useSsl);//use secondary connection string
shardInfo.setPassword("xxxx=");//use secondary access key
//Simple PING command
Jedis jedis=new Jedis(shardInfo.getHost());
try{
jedis.auth(anjiecachekey);
jedis.connect();
System.out.println("My Cache Response: "+jedis.ping());
if (jedis.llen("state") == 0 && jedis.llen("number") == 0) {
for(Map.Entry m: inclass.map.entrySet()){
jedis.lpush("state",(String)m.getValue());
jedis.lpush("number",m.getKey().toString());
}
}
for(String s: jedis.lrange("state", 0, 1000)){
names.add(s);
}
for(String r: jedis.lrange("number", 0, 1000)){
value.add(r);
}
for(int i =0; i < names.size(); i++){
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}
ArrayList<String> states = new ArrayList<String>();
for (Map.Entry m : redisData.entrySet()) {
states.add((String)m.getValue());
}
String[] statesArray = new String[states.size()];
states.toArray(statesArray);
JComboBox<String> stateList = new JComboBox<>(statesArray);
stateList.addItemListener(new MyHandler());
JFrame jframe = new JFrame();
JLabel item1 = new JLabel("IGR Statistics H1 2020");
jframe.add(item1);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new FlowLayout());
jframe.setSize(400,200);
jframe.setVisible(true);
jframe.add(stateList);
// get the selected item:
// String selectedBook = (String) stateList.getSelectedItem();
// check whether the server is running or not
System.out.println("Server is running: " + jedis.ping());
//getting the percentage for each state
// storing the data into redis database
for (Map.Entry m : inclass.map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
}
catch(JedisConnectionException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,"You require a working internet connection");
}
}
static class MyHandler implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
for (Map.Entry m : redisData.entrySet()) {
if(e.getItem().toString() == m.getValue()&& e.getStateChange() == 1){
JOptionPane.showMessageDialog(null, m.getKey(), "VALUE IN BILLIONS", 1);
System.out.println(m.getKey());
break;
}
}
}
}
}
Please how do I correct this error? Any feedback would be highly appreciated. I am new to Java as a language and its syntax. The error also seems to be from my for loops. The error also seems to be from my for loops.
As the error states, you are seeing IndexOutOfBoundsException
. This means that you are trying to access a List's index which more/less than it's range. In this case, value.get(i)
is throwing error:
for (int i = 0; i < names.size(); i++)
{
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}
Try modifying code like below:
for (int i = 0; i < names.size() - 1; i++)
{
redisData.put(Double.parseDouble(value.get(i)), names.get(i));
}
Further, I would suggest you to revisit your java implementation of Redis cache: Intro to Jedis – the Java Redis Client Library