help needed in processing (error)

Would be very greatfull is someone can help me with this, i wanna use my arduino to turn a led on or of via a webpage, so i have some software in a LEDstate.text where i write 0 or 1 to with processing it works as long as the LEDstate.txt is on my hardrive but when its on the server i get the error NullpointerExeption in the processing when i put the link in it, does any know what could be wrong ? I put the processing within.

/*

 A simple Processing script for enabling or disabling the LED on an Arduino using a web interface and serial communication.
 Author: Sindre Lindstad
 Created: 19th of January 2011

 http://projects.sindrelindstad.com

 */

import processing.serial.*;
Serial port;

void setup() {

  /* This part must be altered to fit your local settings. The number in brackets after "Serial.list()" is where you declare what COM port your Arduino is connected to.
   If you get error messages, try a different number starting from 0 (e.g. 0, 1, 2, 3...) . */
  port = new Serial(this, Serial.list()[2], 9600);  // Open the port that the Arduino board is connected to, at 9600 baud
}
void draw() {

  String onoroff[] = loadStrings("https://perswww.kuleuven.be/~u0030608/arduino/LEDstate.txt"); // Insert the location of your .txt file
  print(onoroff[0]);  // Prints whatever is in the file ("1" or "0")

  if (onoroff[0].equals("1") == true) {
    println(" - TELLING ARDUINO TO TURN LED ON");
    port.write('H'); // Send "H" over serial to set LED to HIGH
  } 
  else {

    println(" - TELLING ARDUINO TO TURN LED OFF");
    port.write('L');  // Send "L" over serial to set LED to LOW
  }

  delay(7000); // Set your desired interval here, in milliseconds
}

until you get this to work without throwing an exception,

void setup() {
  size(640, 360);
  background(0); // Set background to black
  String lines[] = loadStrings("https://perswww.kuleuven.be/~u0030608/arduino/LEDstate.txt");
  println("----------------------------");
  println("there are " + lines.length + " lines");
  println("----------------------------");
  for (int i=0; i < lines.length; i++) println(lines[i]);
  println("----------------------------");
}

void draw() {}

your stuff won’t work.

I’m not 100% sure but your challenge might be that your loadStrings command fails to find valid certification path to requested file - can come form the https request but it’s possible that this is due to “Cross-Origin Resource Sharing” - as the application could attempt to load resources from another ‘cross-origin’ domain.

if you control the server, it should be packaging the data (the HTML answer around your ‘1’) with these headers:

Acccess-Control-Allow-Origin: *
Acccess-Control-Allow-Headers: *

or setting up a reverse proxy to bypass the same origin policy (bad idea)

You should google “Processing loadStrings cross domain” to find ideas

You should also be asking Processing questions on the Processing forum, don't you think?

i tested the code above but same error , dont nothing of servers and how they work the only thing is that i can upload files (thats all) :( but i will try to read the topics Processing loadStrings cross domain" and hope i understand it, but already many thanks for advice

basically a java applet if unsigned can only access files within its "domain". this is a security feature to prevent java applet in a browser to do bad stuff.

unsigned applets can make network connections to remote servers (servers that are different from the server that hosts the applet - so your computer in that case) using a special XML file called crossdomain.xml. That file must be accessible on the server that the applet is trying to connect to.

would investigate that possibly

i searched long to solve thus problem but now thanks to people here is see this is a security
problem so i cant use this scrip i quess, is there another way to try to switch on a led and off
from a web page it would be a good practice for me and thanks a lot for the answer

is https://perswww.kuleuven.be your personal web server at an ISP?

if you control the server and can sign the applet, this is solvable.

I can upload my personal website there its a space our compagny gives
kuleuven but i dont control the server

J-M-L:
is https://perswww.kuleuven.be your personal web server at an ISP?

if you control the server and can sign the applet, this is solvable.

can you run PHP there or only host static HTML files?

no idea, here you find the html file :
https://perswww.kuleuven.be/~u0030608/arduino/
here you find the LEDstate.txt:
https://perswww.kuleuven.be/~u0030608/arduino/LEDstate.txt
as i click on or off then i got the something went wrong text.

code for html:

LED ON/OFF

ON /
OFF

Here the php file text:

?php
$onoroff = $_GET[“state”]; // Declares the request from index.html as a variable
$textfile = “https://perswww.kuleuven.be/~u0030608/arduino/”; // Declares the name and location of the .txt file

$fileLocation = “$textfile”;
$fh = fopen($fileLocation, 'w ') or die(“Something went wrong!”); // Opens up the .txt file for writing and replaces any previous content
$stringToWrite = “$onoroff”; // Write either 1 or 0 depending on request from index.html
fwrite($fh, $stringToWrite); // Writes it to the .txt file
fclose($fh);

header(“Location: index.html”); // Return to frontend (index.html)
?>

J-M-L:
can you run PHP there or only host static HTML files?

I have not checked in details but Try in PHP with this

// respond to preflights
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  // return only the headers and not the content
  // only allow CORS if we're doing a GET - i.e. no saving for now.
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: X-Requested-With');
  }
  exit;
}

See Getting CORS Working and the likes such as https://github.com/Luracast/Restler/issues/254

Ideally for security you should not allow origin to be everyone (the *) and use

header('Access-Control-Allow-Origin: http://www.some-site.com');

i copy the code and loaded on the server: https://perswww.kuleuven.be/~u0030608/arduino/test2.php but dont know what to see , sorry dont know nothing of servers, but i appreciate you your help very much !!!

J-M-L: I have not checked in details but Try in PHP with this

// respond to preflights
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  // return only the headers and not the content
  // only allow CORS if we're doing a GET - i.e. no saving for now.
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: X-Requested-With');
  }
  exit;
}

See https://remysharp.com/2011/04/21/getting-cors-working and the likes such as https://github.com/Luracast/Restler/issues/254

Ideally for security you should not allow origin to be everyone (the *) and use

header('Access-Control-Allow-Origin: http://www.some-site.com');