This code works in my friend's pc but I get error

I get ArrayOutOf BoundsException: 0 on line 12 when I run this code, and then java sm has stopped responding when I try to close the small window. Can anyone please help me with this? This same code works for my friend. We both changed the resolution from 1368x928 to 1920x1080.
P.S. I’m not a coder and this is not my code.

import java.awt.Robot; //java library that lets us take screenshots
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import processing.serial.*; //library for serial communication
Serial port; //creates object "port" of serial class
Robot robby; //creates object "robby" of robot class
void setup()
{
  port = new Serial(this, Serial.list()[0],9600); //set baud rate
  size(100, 100); //window size (doesn't matter)
  try //standard Robot class error check
  {
    robby = new Robot();
  }
  catch (AWTException e)
  {
    println("Robot class not supported by your system!");
    exit();
  }
}
void draw()
{
  int pixel; //ARGB variable with 32 int bytes where
  //sets of 8 bytes are: Alpha, Red, Green, Blue
  float r=0;
  float g=0;
  float b=0;
  //get screenshot into object "screenshot" of class BufferedImage
  BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(1368,928)));
  //1368*928 is the screen resolution
  int i=0;
  int j=0;
  //1368*928
  //I skip every alternate pixel making my program 4 times faster
  for(i =0;i<1368; i=i+2){
    for(j=0; j<928;j=j+2){
      pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
      r = r+(int)(255&(pixel>>16)); //add up reds
      g = g+(int)(255&(pixel>>8)); //add up greens
      b = b+(int)(255&(pixel)); //add up blues
    }
  }
  r=r/(684 *464); //average red (remember that I skipped ever alternate pixel)
  g=g/(684 *464); //average green
  b=b/(684 *464); //average blue
  port.write(0xff); //write marker (0xff) for synchronization
  port.write((byte)( r)); //write red value
  port.write((byte)(g)); //write green value
  port.write((byte)(b)); //write blue value
  delay(10); //delay for safety
  background(r,g,b); //make window background average color
}

-a- even it is not your code and you not understand it you might have a idea what it is for?

    • what is connected to serial port
    • why you take screenshots of a PC screen and what you calculate and send?

-b- ports must exist and be free
https://processing.org/reference/libraries/serial/Serial.html
use

  printArray(Serial.list());          // show all ports

-c- posting code here please use the

</> Preformatted text

from the post edit window menu

-d- at beginning use in setup()

frameRate(1); 

to control FPS == lines to port ??
later can optimize or disable.

-e- a little cleanup of your code

import java.awt.Robot;               //java library that lets us take screenshots 
                                     // https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;

import processing.serial.*;           //library for serial communication

Serial port;                          //creates object “port” of serial class
Robot robby;                          //creates object “robby” of robot class

void setup() {
  size(100, 100);                     //window size (doesn’t matter)
  printArray(Serial.list());          // show all ports
  port = new Serial(this, Serial.list()[0], 9600);     //set baud rate
  try {                               //standard Robot class error check
    robby = new Robot();
  }
  catch (AWTException e) {
    println("Robot class not supported by your system!");
    exit();
  }
  frameRate(1);                       // start testing with one line per second send to ?? 
                                      // disable for max ? 60 FPS ( and lines )
}


int pixel;                            //ARGB variable with 32 int bytes where //sets of 8 bytes are: Alpha, Red, Green, Blue
float r=0, g=0, b=0;
BufferedImage screenshot;
int i=0, j=0;
int w = 1368, h = 928;
int d = 2, n=w*h/(d*d);               // w * h = 1269504 n 317376
// https://discourse.processing.org/t/this-code-works-in-my-friends-pc-but-i-get-error/13859
// We both changed the resolution from 1368x928 to 1920x1080.

void draw() {
  background(r, g, b);                //make window background average color
  BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(w, h)));  //1368*928
  for (i =0; i<w; i=i+d) {            //I skip every alternate pixel making my program 4 times faster
    for (j=0; j<h; j=j+d) {
      pixel = screenshot.getRGB(i, j);//the ARGB integer has the colors of pixel (i,j)
      r = r+(int)(255&(pixel>>16));   //add up reds
      g = g+(int)(255&(pixel>>8));    //add up greens
      b = b+(int)(255&(pixel));       //add up blues
    }
  }
  r=r/(n);                            //average red (remember that I skipped ever alternate pixel) 317376
  g=g/(n);                            //average green
  b=b/(n);                            //average blue
  port.write(0xff);                   //write marker (0xff) for synchronization
  port.write((byte)(r));              //write red value
  port.write((byte)(g));              //write green value
  port.write((byte)(b));              //write blue value
  delay(10);                          //delay for safety
}


p.s. your code runs here ( win 10 / processing 3.5.3 / nothing serial connected / original screen size setting )


there are tools so you can readback your screen size and use in the program automatically:

import java.awt.Robot;               //java library that lets us take screenshots 
// https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
                                     // kll added for screen shot file and auto screen size adjust
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import processing.serial.*;           //library for serial communication

Serial port;                          //creates object “port” of serial class
Robot robby;                          //creates object “robby” of robot class
Dimension myscreen;
Rectangle screenRect;
// https://discourse.processing.org/t/this-code-works-in-my-friends-pc-but-i-get-error/13859
// We both changed the resolution from 1368x928 to 1920x1080.
int w = 1368, h = 928;
int d = 2, n=w*h/(d*d);               // w * h = 1269504 n 317376

int pixelc;                            //ARGB variable with 32 int bytes where //sets of 8 bytes are: Alpha, Red, Green, Blue
float r=0, g=0, b=0;
BufferedImage screenshot;
int i=0, j=0;

void setup() {
  size(300, 100);                     //window size (doesn’t matter)
  printArray(Serial.list());          // show all ports
  port = new Serial(this, Serial.list()[0], 9600);     //set baud rate
  try {                               //standard Robot class error check
    robby = new Robot();
  }
  catch (AWTException e) {
    println("Robot class not supported by your system!");
    exit();
  }
  //frameRate(1);                       // start testing with one line per second send to ?? // disable for max ? 60 FPS ( and lines )
  myscreen = Toolkit.getDefaultToolkit().getScreenSize();
  screenRect = new Rectangle(myscreen);
  w = myscreen.width;
  h = myscreen.height;
  n = w*h/(d*d);
  println(w +"*"+ h+" w*h "+w*h+" n "+n);
}

void draw() {
  background(r, g, b);                //make window background average color
  surface.setTitle(nf(frameRate,0,1)+"FPS");
  screenshot = robby.createScreenCapture(new Rectangle(new Dimension(w, h)));  //1368*928
  for (i =0; i<w; i=i+d) {            //I skip every alternate pixel making my program 4 times faster
    for (j=0; j<h; j=j+d) {
      pixelc = screenshot.getRGB(i, j);//the ARGB integer has the colors of pixel (i,j)
      r = r+(int)(255&(pixelc>>16));   //add up reds
      g = g+(int)(255&(pixelc>>8));    //add up greens
      b = b+(int)(255&(pixelc));       //add up blues
    }
  }
  r=r/(n);                            //average red (remember that I skipped ever alternate pixel) 317376
  g=g/(n);                            //average green
  b=b/(n);                            //average blue
  port.write(0xff);                   //write marker (0xff) for synchronization
  port.write((byte)(r));              //write red value
  port.write((byte)(g));              //write green value
  port.write((byte)(b));              //write blue value
  delay(10);                          //delay for safety
}

void keyPressed() {
  if ( key == 's' ) {
    try {
      ImageIO.write(screenshot, "png", new File(sketchPath("")+'/'+"data/robotshot.png"));
      println("saved");
    } 
    catch ( IOException  ex) {  
      println(ex);
    }
  }
}

Did you install the robot library?

Check the port the Arduino is on

The code is used for basically getting average colour of the screen and send it to the arduino. I’m trying to make ambilight with this setup and this code was given in a tutorial. I’m not sure if it’s ok to link external sites here or I would’ve linked it.

The problem is, this code is supposed to work weather arduino is connected or not, atleast the avg screen colour display part. In my case it simply doesnt work.

-a- so on my Win 10 PC there is a

[0] "COM1"

without any device connected
and your code starts without error
if NOT you need a Arduino connected.

so you learn how to disable ( make switchable ) the code part
what causes that error

-b- “ambilight with Arduino”
would have been a good name for your topic,
and might have attracted someone who did that already.

-c- But if the error has to do with the PC screen size,
did you try my latest code version with auto adjust?

-d- if you get (original) code from a other source ( like tutorial and blog )
a link would be good, actually it should be inside your code
even if it is not by authors copyright required, just for respect…

Did you install the robot library?

import java.awt.Robot;               //java library that lets us take screenshots 

does not need any add install here ( win 10 / processing 3.5.3 )
please try on your system.