loadStrings problem [2]. Throwing nullPointer?

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;

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:

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

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

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

Kf

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.

Hopefully this helps,
EnhancedLoop7

@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.

@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.

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

Yes it still throws the exception:(

@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…

This is the whole code found in MainActivity:

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:

 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

@marcg===

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

@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.

    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();
1 Like

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

@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?

@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().

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:

    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?

@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).