# loadStrings problem \[2\]. Throwing nullPointer?

**URL:** <https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569>\
**Category:** Processing for Android\
**Created:** [September 16, 2018, 10:59am UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569 "2018-09-16T10:59:01Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 16, 2018, 10:59am UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/1 "2018-09-16T10:59:01Z")

</div>

So my last thread was all about networking in android, not really about loadStrings. This thread is.  
In my MainActivity I’m trying to use the loadStrings function inside a new thread;

```auto
Thread thread = new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          coord = sketch.loadStrings("https://pastebin.com/raw/git52SPJ");
          elemente = sketch.loadStrings("https://pastebin.com/raw/xpZNUzCW");
          seed = sketch.loadStrings("https://pastebin.com/raw/VWZk096D");
          sketch.println("aaamarc");
        } catch (Exception ex) {
          sketch.println("aamarc", ex);
          ex.printStackTrace();
        }
      }
    });

```

The problem is that the console prints this:

```auto
12955-12955/processing.test.skbun I/System.out: aamarc java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] processing.core.PApplet.loadStrings(java.lang.String)' on a null object reference

```

Why is the loadStrings function throwing this exception? The coord, elemente and seed strings[] are declared above onCreate() like this, so they can be accessed in the sketch.java

```auto
private PApplet sketch;
  public static String[] seed;
  public static String[] elemente;
  public static String[] coord;

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [September 16, 2018, 3:05pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/2 "2018-09-16T15:05:59Z")

</div>

You need to show all your code or an MCVE. Are you doing this from Processing or AS?

Kf

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [September 16, 2018, 10:21pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/3 "2018-09-16T22:21:14Z")

</div>

Hello, the null pointer isn’t because you didn’t declare your variables or anything like that, that part from what I can see is correct. The issue must be the links you are passing in. The null pointer basically means that you are trying to have your program work with something, that “doesn’t exist”. So your program says, “where is it? I can’t find it!” and then shows the null pointer error. I would suggest at first, checking your links?

**Edit**

Also, forgive me if I’m wrong on this but are you supposed to have the “sketch” prefix for everything? Maybe you can just try `loadStrings(your url);` That, I got from looking at [the reference for loadStrings.](https://processing.org/reference/loadStrings_.html)

Hopefully this helps,  
EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 8:55am UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/4 "2018-09-17T08:55:53Z")

</div>

@marcg ===  
as @kfrajer i dont know wether you are working wit P5+AS or P5 only (you are speaking about on create && main activity, which means AS but it could be also p5 android 2X…) that does not matter here= the error is about your arrays which are null and looking to the url provided i think that they are not good for the loadStrings() method which is looking for a file with .txt or html sufix. Try with another url pointing towards some .txt: your code will work.

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 2:48pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/5 "2018-09-17T14:48:30Z")

</div>

@EnhancedLoop7 I use the prefixes because I execute this code in the activity, not in the sketch.java.  
@akenaton if I try to do this in processing, not in android, it works just fine with these links.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 2:52pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/6 "2018-09-17T14:52:11Z")

</div>

@marcg===  
i have not tried with java; but have you tried giving a file (.txt or .json ) at some url with android?

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 3:14pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/7 "2018-09-17T15:14:12Z")

</div>

Yes it still throws the exception:(

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 3:17pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/8 "2018-09-17T15:17:05Z")

</div>

@marcg  
which e? array?

- can you post some code ? at what time are you calling loadStrings()? - As for me i have tried with AS and it works…

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 3:21pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/9 "2018-09-17T15:21:43Z")

</div>

This is the whole code found in MainActivity:

```auto
package processing.test.skbun;

import android.os.Bundle;
import android.content.Intent;
import android.os.StrictMode;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import processing.android.PFragment;
import processing.android.CompatUtils;
import processing.core.PApplet;

public class MainActivity extends AppCompatActivity {
  private PApplet sketch;
  public static String[] seed;
  public static String[] elemente;
  public static String[] coord;
  //static List<String> coord = new ArrayList<>();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
// StrictMode.setThreadPolicy(policy);
    //
    Thread thread = new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          sketch.println("aaamarc0");
          //coord.addAll(sketch.loadStrings("https://pastebin.com/raw/git52SPJ"));
          coord = sketch.loadStrings("http://textfiles.com/internet/aboutmtv.txt");
          sketch.println("aaamarc1");
          elemente = sketch.loadStrings("https://pastebin.com/raw/xpZNUzCW");
          sketch.println("aaamarc2");
          seed = sketch.loadStrings("https://pastebin.com/raw/VWZk096D");
          sketch.println("aaamarc3");
        } catch (Exception ex) {
          sketch.println("aamarc", ex);
          ex.printStackTrace();
        }
      }
    });

    thread.start();

// coord = sketch.loadStrings("https://pastebin.com/raw/git52SPJ");
// elemente = sketch.loadStrings("https://pastebin.com/raw/xpZNUzCW");
// seed = sketch.loadStrings("https://pastebin.com/raw/VWZk096D");
    sketch = new skBun();
    PFragment fragment = new PFragment(sketch);
    fragment.setView(frame, this);
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (sketch != null) {
      sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  }

  @Override
  public void onNewIntent(Intent intent) {
    if (sketch != null) {
      sketch.onNewIntent(intent);
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (sketch != null) {
      sketch.onActivityResult(requestCode, resultCode, data);
    }
  }

  @Override
  public void onBackPressed() {
    if (sketch != null) {
      sketch.onBackPressed();
    }
  }
}

```

And this is the log:

```auto
 I/System.out: aaamarc0
 I/System.out: aamarc java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] processing.core.PApplet.loadStrings(java.lang.String)' on a null object reference

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 3:38pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/10 "2018-09-17T15:38:55Z")

</div>

@marcg===

try with this url=  
[http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt](http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt)

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 3:51pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/11 "2018-09-17T15:51:15Z")

</div>

@akenaton I got it. the PApplet sketch was not initialized, meaning that it was an empty variable, so I fixed the problem by initializing it as a new PApplet(), and after I got my values I reinitialized it with the .java file.

```auto
    sketch = new PApplet();
    coord = sketch.loadStrings("https://pastebin.com/raw/git52SPJ");
    elemente = sketch.loadStrings("https://pastebin.com/raw/xpZNUzCW");
    seed = sketch.loadStrings("https://pastebin.com/raw/VWZk096D");
    sketch = new skBun();

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 3:58pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/12 "2018-09-17T15:58:13Z")

</div>

@marcg===  
right, i thought to that, but how do you explain that your println (the first one) appears without firing error?

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 4:28pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/13 "2018-09-17T16:28:08Z")

</div>

@akenaton damn. it’s so strange that sometimes it works, but after I try it again it failes. I don’t know, maybe a loadStrings error?

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 4:42pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/14 "2018-09-17T16:42:46Z")

</div>

@marcg====

i suppose that you are using the P5 android mode (inside AS) for \>3XX (before, at start it was an activity, now this activity creates a fragment (PFragment) which extends native Fragment )- In this case i dont understand your code and its aim; i have tried WITHOUT AS and loadStrings() works when it finds some file at the URL; but i have seen that some of your URL do not work (eg: the /internet one): even when i go to this url with a browser i cannot see it, i can only DOWNLOAD it, for seing i have to display the source code: as i am not an expert with web i cannot explain but i think that this an error source. so : first thing verify that you can see and read the files you want to download when you go (browser) to the urls; second thing try loadStrings() in the most simple way out of AS; thirdly: try to deplace this method in the PFragment (calling it in setup or onStart().

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [September 17, 2018, 4:50pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/15 "2018-09-17T16:50:31Z")

</div>

first, if I try using loadStrings in the actual sketch it works without problems, but unfortunately the current project I’m working requires that loadStrings works in other activities, not just in the sketch.

second, in processing ide, all this code works without a single problems by the loadStrings method.

third, I use just pastern raw url’s (which work in the P IDE!!)

these are the 3 url’s I’m actually testing this:

```auto
    coord = sketch.loadStrings("https://pastebin.com/raw/SvJBStEu");
    elemente = sketch.loadStrings("https://pastebin.com/raw/WYAzeQAg");
    seed = sketch.loadStrings("https://pastebin.com/raw/uaSFKaTh");

```

4th thing: I know it’s extending the fragment, exactly which part of my code you don’t see it’s aim?

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [September 17, 2018, 5:03pm UTC](https://discourse.processing.org/t/loadstrings-problem-2-throwing-nullpointer/3569/16 "2018-09-17T17:03:53Z")

</div>

@marcg===  
i have to test your code with AS; i ll do that when i get some time…  
if you have other activities i understand now why you want to load these values at launch and have them static. Yet if it does not work (or work only some times!) i think you can leave the vars as they are, launch the fragment, get them inside it and pass them to the main activity or save them inside your sketch (with AS: in the raw folder).
