# Solution: How to check for internet-connection in Processing

**URL:** <https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772>\
**Category:** Gallery\
**Created:** [March 29, 2019, 8:08pm UTC](https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772 "2019-03-29T20:08:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [March 29, 2019, 8:08pm UTC](https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772/1 "2019-03-29T20:08:17Z")

</div>

Hi!  
I was trying to figure out how to check if a device is connected to the internet, but couldn’t find any helpful results on google ([this](https://forum.processing.org/two/discussion/3170/how-to-check-if-i-have-an-internet-connection-inetaddress-isreachable) is the only thing I found). I eventually figured it out, and wanted to share my code, so that other beginners like me won’t have to google for hours. I’ve seen other people post stuff like this, so I hope this is not considered as a waste of space or anything.

```auto
boolean isConnected() {
  String[] file; //creates a String that will be loaded with stuff from the internet
  try {
    //load a text file from somewhere on the internet
    file = loadStrings("YourURLWithAFileExtensionLike.txtAtTheEnd");
    //TRY to print it --> throws NullPointerException if it was not loaded correctly
    println(file[0]); 
  } 
  catch (NullPointerException e) { //if there is an exception, do this:
    println("Connection is not available...");
    return false; //function returns false --> not connected
  }
  return true; //function returns true --> connected; I can put this here,
               //because the "return" earlier immediately exits the function,
               //so that this will never be called, unless there is a connection
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 30, 2019, 2:30am UTC](https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772/2 "2019-03-30T02:30:54Z")

</div>

Can’t you simply check for `null` rather than `try/catch` it? 🤔

```auto
boolean isConnected() {
  return loadStrings("SomeURL.txt") != null;
}

```

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [March 30, 2019, 7:05am UTC](https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772/3 "2019-03-30T07:05:46Z")

</div>

Um, maybe… 😅  
The way I tried checking for `null` before resulted in a crash, and I thought this was the only solution. Anyways, now there is an even shorter solution online, so thank you! 😛

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 30, 2019, 9:44pm UTC](https://discourse.processing.org/t/solution-how-to-check-for-internet-connection-in-processing/9772/4 "2019-03-30T21:44:06Z")

</div>

This is taking advantage of the way that loadStrings already handles URLs – or files, for that matter, anything that is / is not available:

> If the file is not available or an error occurs, **null** will be returned and an error message will be printed to the console. [loadStrings() / Reference / Processing.org](https://processing.org/reference/loadStrings_.html)

Note however that this isn’t exactly the same as checking for an internet connection. Depending on your network / configuration, there might be a long delay for a timeout on a loadStrings url request.
