0

I have a problem, when i running this code, i found this problem. how to fix it ?

this code

      

      public class kpi {
      static String host = "";
      
      public static void main(String[] args) {
        host = args[0].toLowerCase();
        try {
          SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
          Date now = new Date();
          String strDate = sdfDate.format(now);
          JSch jsch = new JSch();
          Session session = jsch.getSession("hs", args[1], 22);
          session.setConfig("StrictHostKeyChecking", "no");
          session.setPassword("kti365LM");
          session.connect();
          System.out.println("session connected");
          Channel channel = session.openChannel("shell");
          OutputStream ops = channel.getOutputStream();
          PrintStream ps = new PrintStream(ops, true);
          channel.connect();
          System.out.println("channel connected");
          String prompt = "[local]" + args[0] + "#";
          System.out.println(exec(channel, ps, "epg node status", prompt));
          String nodeinfo = "";
          nodeinfo = exec(channel, ps, "pdc_kpi.pl", prompt);
          String[] linenode = nodeinfo.split("\\r?\\n");
          String key = "";
          JSONObject peerjson = new JSONObject();
          peerjson.put("time", strDate.replace(" ", "T"));
          peerjson.put("node", args[0]);
          for (int i = 6; i  0 && c  0) {`
                    Pattern p = Pattern.compile("(\\d+(?:\\.\\d+)?)");
                    Matcher m = p.matcher(fields[j]);
                    if (m.find()) {
                      System.out.println(key + ":" + m.group());
                      peerjson.put(key, Float.parseFloat(m.group().trim()));
                      c++;
                    } 
                  } 
                  key = key + fields[j];
                  c++;
                } 
                if (j > 4 && c == 0)
                  break; 
              } 
              key = "";
            } 
          } 
          insert(strDate, "nodekpimme", peerjson.toString());
          channel.disconnect();
          session.disconnect();
        } catch (Exception e) {
          e.printStackTrace();
        } 
      }

and this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at trcrt.kpi.main(kpi.java:24)

acang
  • 3
  • 3

3 Answers3

0

Index 0 would be first element but you have no elements in your args.You are probably running your app without arguments. You need to pass them to args variable if you want them to be available.

Vojin Purić
  • 2,140
  • 7
  • 9
  • 22
  • I've found the answer to this case.Turns out the problem was an incorrect JDK version. when I changed JDK version and it worked. thank you – acang Aug 01 '22 at 12:18
0

Your code is a lot messy. I think i = 6, why is 6 here. You should carefully trace get the value of fields[j]. You can finally get the answer

Hoi Tran
  • 11
  • 3
  • for (int i = 6; i < linenode.length - 2; i++) { if (!linenode[i].contains("=")) { String[] fields = linenode[i].split(" "); int c = 0; for (int j = 0; j < fields.length; j++) { if (fields[j].length() > 0 && c < 3) { if (c > 0) { Pattern p = Pattern.compile("(\\d+(?:\\.\\d+)?)"); Matcher m = p.matcher(fields[j]); if (m.find()) { System.out.println(key + ":" + m.group()); peerjson.put(key, Float.parseFloat(m.group().trim())); – acang Jul 01 '22 at 10:58
0

You're probably running without passing any argument to you main(). Try to catch this error by surrounding "host = args[0].toLowerCase();" in a try-catch block or even better control the existence of args and args.length>1 and return with an error message if the args is empty.

babatto
  • 66
  • 1
  • 3