Catching InvocationTargetException

need your help, gyus

inb4 i know that code is huge pile of bs but i need it to work

catching a lot of errors trying to pass lorem array into load.dissasemble2array

main

int lngth = 10000; 
int scaleX = 5;
int scaleY = 2;
String fileName = new String();
String[] lorem = new String[lngth];
int[][] endArray = new int[lngth][10];
int[][] smoothArray = new int[lngth][10];
drawGraph graph;
fileLoad load;
void setup() {
  noLoop();
  size(1800, 600);
  graph = new drawGraph();
  selectInput("Select a file to graph: ", "fileSelected");
}
void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    exit();   
  } else {
    println(selection.getAbsolutePath());
    fileName = selection.getAbsolutePath();
    println(fileName);
    lorem = loadStrings(fileName);
    endArray = load.disassemble2array(lorem);
    smoothArray = load.smoothen(endArray);
  }
}
void draw() { 
  background(0); 
  stroke(50);
  fill(255);
  for (int i = 0; i < height; i = i+scaleY*10) {
    line(20, height - i, width, height - i);
    text(i / scaleY, 5, height - i);
  }
  stroke(255);
  graph.drawAll(smoothArray, lngth, scaleX, scaleY);
}
void mousePressed() {
  if ((scaleX-1 != 0) && (scaleY+1 != 0) && (mouseButton == LEFT)) {
    scaleX--;
    scaleY++;
    redraw();
  } else if ((scaleX+1 != 0) && (scaleY-1 != 0) && (mouseButton == RIGHT)) {
    scaleY--;
    scaleX++;
    redraw();
  }
}

fileLoad

class fileLoad {
  int k = 0;
  int s = 0;
  int p = 0;
  int last_s;
  int[][] disassemble2array(String[] lorem) {
    //String[] lorem = loadStrings(fileName);
    int[][] temp = new int[lorem.length][10];
    for (int i = 0; i < lorem.length - 1; i++) {
      last_s = 0;
      for (s = 0; s < lorem[i].length() - 1; s++) {
        if (lorem[i].charAt(s) == ';') {
          temp[i][p] = int(lorem[i].substring(last_s, s));
          //println(temp[i][p]);
          last_s = s + 1;
          p++;
        }
      }
      temp[i][p] = int(lorem[i].substring(last_s, s+1));
      //println(temp[i][p]);
      //println();
      s = p = 0;
    }
    return temp;
  }
  int[][] smoothen(int[][] arr) {
    for (int i = 12; i < arr[i].length - 12; i++) {
      for (int col = 1; col < arr[i].length-1; col++) {
        arr[i][col] = (arr[i-12][col] + arr[i-11][col] + arr[i-10][col] + arr[i-9][col] + arr[i-8][col] + arr[i-7][col] + arr[i-6][col] + arr[i-5][col] + arr[i-4][col] + arr[i-3][col] + arr[i-2][col] + arr[i-1][col] + arr[i][col] + arr[i+1][col] + arr[i+2][col] + arr[i+3][col] + arr[i+4][col] + arr[i+5][col] + arr[i+6][col] + arr[i+7][col] + arr[i+8][col] + arr[i+9][col] + arr[i+10][col] + arr[i+11][col] + arr[i+12][col])/25;
      }
    }
    return arr;
  }
}

errors

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at processing.core.PApplet.selectCallback(PApplet.java:6484)
    at processing.core.PApplet.access$0(PApplet.java:6477)
    at processing.core.PApplet$3.run(PApplet.java:6389)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.NullPointerException
    at test_file_sketch.fileSelected(test_file_sketch.java:53)
    ... 21 more
Tagged:

Answers

  • Can you provide your text file?

    Do you get the error after you load your file?

    Kf

  • text file is just csv

    1;53;54;55;56;57;58;59;60;59
    2;56;57;58;59;60;61;62;63;128
    3;59;60;61;62;63;64;65;66;142
    4;62;63;64;65;66;67;68;69;88
    5;64;65;66;67;68;69;70;71;39
    6;66;67;68;69;70;71;72;73;75
    7;68;69;70;71;72;73;74;75;72
    8;69;70;71;72;73;74;75;76;113
    9;70;71;72;73;74;75;76;77;119
    10;70;71;72;73;74;75;76;77;62
    11;70;71;72;73;74;75;76;77;99
    12;69;70;71;72;73;74;75;76;87
    13;68;69;70;71;72;73;74;75;83
    14;66;67;68;69;70;71;72;73;68
    15;64;65;66;67;68;69;70;71;145
    16;61;62;63;64;65;66;67;68;76
    17;58;59;60;61;62;63;64;65;37
    18;55;56;57;58;59;60;61;62;43
    19;52;53;54;55;56;57;58;59;39
    20;49;50;51;52;53;54;55;56;91
    21;46;47;48;49;50;51;52;53;122
    22;43;44;45;46;47;48;49;50;66
    23;40;41;42;43;44;45;46;47;15
    24;37;38;39;40;41;42;43;44;21
    25;35;36;37;38;39;40;41;42;47
    26;33;34;35;36;37;38;39;40;43
    27;32;33;34;35;36;37;38;39;74
    28;31;32;33;34;35;36;37;38;20
    29;30;31;32;33;34;35;36;37;66
    30;30;31;32;33;34;35;36;37;73
    

    and yes, file gets loaded and error pops after

  • Answer ✓

    Notice three major changes below. The definition of disassemble2arrayBB() was just an extra step but probly not needed.

    Kf

    int lngth = 10000; 
    int scaleX = 5;
    int scaleY = 2;
    String fileName = new String();
    String[] lorem = new String[lngth];
    int[][] endArray = null;//new int[lngth][10];     //CHANGED:
    int[][] smoothArray = new int[lngth][10];
    //drawGraph graph;
    fileLoad load;
    
    
    void setup() {
      noLoop();
      size(1800, 600);
      //graph = new drawGraph();
      selectInput("Select a file to graph: ", "fileSelected", new File(dataPath("")));
    }
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
        exit();
      } else {
        println(selection.getAbsolutePath());
        fileName = selection.getAbsolutePath();
        println(fileName);
      }
    }
    void draw() { 
      background(0); 
      stroke(50);
      fill(255);
    
      if (fileName.equals("")==false && endArray==null) {   //CHANGED: This block will execute only once
        endArray = new int[lngth][10];
        lorem = loadStrings(fileName);
        endArray = load.disassemble2array(lorem);
        smoothArray = load.smoothen(endArray);
      }
    
      for (int i = 0; i < height; i = i+scaleY*10) {
        line(20, height - i, width, height - i);
        text(i / scaleY, 5, height - i);
      }
      stroke(255);
      //graph.drawAll(smoothArray, lngth, scaleX, scaleY);
    }
    void mousePressed() {
      if ((scaleX-1 != 0) && (scaleY+1 != 0) && (mouseButton == LEFT)) {
        scaleX--;
        scaleY++;
        redraw();
      } else if ((scaleX+1 != 0) && (scaleY-1 != 0) && (mouseButton == RIGHT)) {
        scaleY--;
        scaleX++;
        redraw();
      }
    }
    
    
    class fileLoad {
      int k = 0;
      int s = 0;
      int p = 0;
      int last_s;
    
      int[][] disassemble2arrayBB(String[] lorem) {    //CHANGED:
        int[][] temp = new int[lorem.length][10];
    
        for (int i = 0; i < lorem.length; i++) {
          int[] val0=int(split(lorem[i], ';'));
          for (int j=0; j<val0.length; j++) {
            temp[i][j]=val0[j]; 
          }
        }
        return temp;
      }
    
      int[][] disassemble2array(String[] lorem) {
        //String[] lorem = loadStrings(fileName);
        int[][] temp = new int[lorem.length][10];
        for (int i = 0; i < lorem.length - 1; i++) {
          last_s = 0;
          for (s = 0; s < lorem[i].length() - 1; s++) {
            if (lorem[i].charAt(s) == ';') {
              temp[i][p] = int(lorem[i].substring(last_s, s));
              //println(temp[i][p]);
              last_s = s + 1;
              p++;
            }
          }
          temp[i][p] = int(lorem[i].substring(last_s, s+1));
          //println(temp[i][p]);
          //println();
          s = p = 0;
        }
        return temp;
      }
      int[][] smoothen(int[][] arr) {
        for (int i = 12; i < arr[i].length - 12; i++) {
          for (int col = 1; col < arr[i].length-1; col++) {
            arr[i][col] = (arr[i-12][col] + arr[i-11][col] + arr[i-10][col] + arr[i-9][col] + arr[i-8][col] + arr[i-7][col] + arr[i-6][col] + arr[i-5][col] + arr[i-4][col] + arr[i-3][col] + arr[i-2][col] + arr[i-1][col] + arr[i][col] + arr[i+1][col] + arr[i+2][col] + arr[i+3][col] + arr[i+4][col] + arr[i+5][col] + arr[i+6][col] + arr[i+7][col] + arr[i+8][col] + arr[i+9][col] + arr[i+10][col] + arr[i+11][col] + arr[i+12][col])/25;
          }
        }
        return arr;
      }
    }
    
  • edited June 2017

    thanks kfrajer errors are gone

    but now it seems like file does not actually loading

    full code down here, it should draw graphs based on data readings from file

    int lngth = 10000; 
    
    int scaleX = 5;
    int scaleY = 2;
    
    String fileName = new String();
    String[] lorem = new String[lngth];
    
    int[][] endArray = null;
    int[][] smoothArray = new int[lngth][10];
    
    drawGraph graph;
    fileLoad load;
    
    void setup() {
      noLoop();
      size(1800, 600);
      selectInput("Select a file to graph: ", "fileSelected", new File(dataPath("")));
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
        exit();
      } else {
        println(selection.getAbsolutePath());
        fileName = selection.getAbsolutePath();
        println(fileName);
      }
    }
    
    void draw() { 
      background(0); 
      stroke(50);
      fill(255);
    
      if (fileName.equals("")==false && endArray==null) {   //CHANGED: This block will execute only once
        endArray = new int[lngth][10];
        lorem = loadStrings(fileName);
        println("load succ");
        endArray = load.disassemble2array(lorem);
        smoothArray = load.smoothen(endArray);
      }
    
      for (int i = 0; i < height; i = i+scaleY*10) {
        line(20, height - i, width, height - i);
        text(i / scaleY, 5, height - i);
      }
      stroke(255);
      graph.drawAll(smoothArray, lngth, scaleX, scaleY);
    }
    
    void mousePressed() {
      if ((scaleX-1 != 0) && (scaleY+1 != 0) && (mouseButton == LEFT)) {
        scaleX--;
        scaleY++;
        redraw();
      } else if ((scaleX+1 != 0) && (scaleY-1 != 0) && (mouseButton == RIGHT)) {
        scaleY--;
        scaleX++;
        redraw();
      }
    }
    
    
    class fileLoad {
      int k = 0;
      int s = 0;
      int p = 0;
      int last_s;
    
      int[][] disassemble2array(String[] lorem) {
        //String[] lorem = loadStrings(fileName);
        int[][] temp = new int[lorem.length][10];
        for (int i = 0; i < lorem.length - 1; i++) {
          last_s = 0;
          for (s = 0; s < lorem[i].length() - 1; s++) {
            if (lorem[i].charAt(s) == ';') {
              temp[i][p] = int(lorem[i].substring(last_s, s));
              //println(temp[i][p]);
              last_s = s + 1;
              p++;
            }
          }
          temp[i][p] = int(lorem[i].substring(last_s, s+1));
          //println(temp[i][p]);
          //println();
          s = p = 0;
        }
        return temp;
      }
    
      int[][] smoothen(int[][] arr) {
        for (int i = 12; i < arr[i].length - 12; i++) {
          for (int col = 1; col < arr[i].length-1; col++) {
            arr[i][col] = (arr[i-12][col] + arr[i-11][col] + arr[i-10][col] + arr[i-9][col] + arr[i-8][col] + arr[i-7][col] + arr[i-6][col] + arr[i-5][col] + arr[i-4][col] + arr[i-3][col] + arr[i-2][col] + arr[i-1][col] + arr[i][col] + arr[i+1][col] + arr[i+2][col] + arr[i+3][col] + arr[i+4][col] + arr[i+5][col] + arr[i+6][col] + arr[i+7][col] + arr[i+8][col] + arr[i+9][col] + arr[i+10][col] + arr[i+11][col] + arr[i+12][col])/25;
          }
        }
        return arr;
      }
    }
    
    
    class drawGraph {
      
      color[] colarray = { color(255,204,0), color(91,100,110), color(185,65,200), color(0,145,35), color(245,35,200), color(108,122,89), color(0,33,55), color(255,79,0), color(202,58,39), color(127,255,212), color(255,0,0) };
    
      void drawAll(int [][] arr, int lngth, int scaleX, int scaleY) {
        for (int i = 0; i < lngth; i = i+scaleX) {
          for (int column = 1; column < arr[i].length-1; column++) {
            stroke(colarray[column]);
            fill(colarray[column]);
            ellipse(i/scaleX, height - arr[i][column]*scaleY, 2, 2);
          }
        }
      }
    }
    
Sign In or Register to comment.