CSV file not found in android but no problem in processing 3

edited May 2018 in Android Mode

I started with a demo that uses a menu that allows to choose which sketch to run on android. I modified it to add my sketch to the menu. I took my sketch that works standalone on android and added it to the demo. My sketch reads a csv file and uses the data as input. Heres the link to the processing android demo: https://github.com/omerjerk/ProcessingAndroidDemo

When I add my sketch and the csv file to a /data directory, it crashes and I get a blue processing error message that says make sure the file URL is accessible or readable. The Java error is Nullpointerexception trying to determine the length of a null array. I tried putting the csv file in different project directories but it doesn't matter. I'm using Android studio if it matters.

Heres my code that I put in draw():

    String[] starLines = loadStrings("stardata.csv");
    println("Loaded " + starLines.length + " stars:");

    Lat = (float)Latitude_1 + (float)Lat_minutes/60 + (float)Lat_seconds/3600;  //Estimated Latitude
    Lon = (float)Longitude_1 - (float)Lon_minutes/60 - (float)Lon_seconds/3600; //Estimated Longitude

   if (Longitude_1 > 0) {
   Lon = (float)Longitude_1 + (float)Lon_minutes/60 + (float)Lon_seconds/3600; //East of Greenwich
   }

    for ( i = 0 ; i < starLines.length; i++) {

        String[] stars = splitTokens(starLines[i], ", ");
        String proper = (stars[0]);
        String con = (stars[1]);
        double dec = PApplet.parseFloat(stars[2]);
        double ra = PApplet.parseFloat(stars[3]);
        double pmra = PApplet.parseFloat(stars[4]);
        double pmdec = PApplet.parseFloat(stars[5]);
        double rv = PApplet.parseFloat(stars[6]);

        ra = ra * 15;

        Star1 = Name1.getCaptionLabel().getText();
        if (Star1 == "Procyon") {
            z = 47;

....

Any ideas where stardata.csv should go in the project, so it is "accessible" ?

Answers

  • @HarryCodes===

    Put your file in the assets folder (create it); then get it using AssetManager

  • edited May 2018

    Strange, I added the csv file to the assets folder and it didn't read it. I added an assets folder inside the src folder and it read it without any additional code.

    The problem now is that I'm using a library to work on the data from the csv and it says NullPointerException lock == null. Whole different problem now.

  • edited May 2018

    @HarryCodes===

    AssetManager can be used for fragment. Can you put the code you are using?

  • I'm using the JPARSEC library, for astronomy. this link goes to the general page: conga.oan.es/~alonso/doku.php?id=jparsec#jparsec_for_android_platform

    I am using the jparsec directory from the android clearsky source.

  • edited May 2018

    Here's the code for my sketch that I added to the processing android demo. The problem starts with the ObserverElement. It seems to be trying to read another file using Reader.java

    I think I see why its not working with the demo. The difference is that the demo uses gradle and my sketch that worked was jammed into a project(the clearsky android example) imported from eclipse. Theres some kind of dependency issue maybe.

    package in.omerjerk.processingdemo.sketch;
    
    import java.util.Arrays;
    import java.util.List;
    
    import controlP5.ControlP5;
    import controlP5.DropdownList;
    import controlP5.ScrollableList;
    import controlP5.Textarea;
    import jparsec.ephem.Ephem;
    import jparsec.ephem.EphemerisElement;
    import jparsec.ephem.EphemerisElement.COORDINATES_TYPE;
    import jparsec.ephem.EphemerisElement.FRAME;
    import jparsec.ephem.Functions;
    import jparsec.ephem.Nutation;
    import jparsec.ephem.Target.TARGET;
    import jparsec.ephem.planets.EphemElement;
    import jparsec.ephem.stars.StarElement;
    import jparsec.ephem.stars.StarEphem;
    import jparsec.ephem.stars.StarEphemElement;
    import jparsec.io.ConsoleReport;
    import jparsec.math.Constant;
    import jparsec.observer.LocationElement;
    import jparsec.observer.ObserverElement;
    import jparsec.time.AstroDate;
    import jparsec.time.SiderealTime;
    import jparsec.time.TimeElement;
    import jparsec.time.TimeElement.SCALE;
    import jparsec.time.TimeScale;
    import processing.core.PApplet;
    import processing.core.PFont;
    
    //import jparsec.ephem.IAU2006;
    //import jparsec.ephem.IAU2006.CIO_EPHEMERIS;
    //import jparsec.ephem.planets.JPLEphemeris;
    //import jparsec.math.matrix.Matrix;
    //import jparsec.observer.EarthOrientationParameters;
    
    public class CSVTest2 extends PApplet {
    
    
    
    
    //import java.sql.*;
    //import de.bezier.data.sql.*;
    
    //import java.lang.*;
    
    //import java.time.*;
    
    //SQLite db;
    ControlP5 cp5;
    ControlP5 controlP5;
    Textarea myTextarea;
    
    int Latitude_1 = 39;
    int Lat_minutes = 20;
    int Lat_seconds = 10;
    int Longitude_1 = -77;
    int Lon_minutes = 00;
    int Lon_seconds = 26;
    float Lon = 0;
    float Lat = 0;
    
    //ScrollableList Name1;
        DropdownList Name1;
    ScrollableList Name2;
    String Star1 = " ";
    String Star2 = " ";
    
    String starName = " ";
    float ra = 0;
        String proper = " ";
    float RA = 0;
    float dec = 0;
    double pmra = 0;
    double pmdec = 0;
    double rv = 0;
        int i = 0 ;
    PFont f;
    int z = 18;
        int index = 3;
        int timer;
        int wait = 10000;
    
    
    
    
    public void setup()
    {
     //  size( 100, 100 );
    
    
     f = createFont("Arial",16,true); 
    //  db = new SQLite( this, "hygdata.db" );  // open database file
    
      cp5 = new ControlP5(this);
      controlP5 = new ControlP5(this);
                   List l = Arrays.asList("Procyon","Rasalhague","Dubhe", "Mirphak", "Alkaid","Altair", "Alioth", "Spica", "Betelgeuse", "Capella", "Vega","Polaris", "Menkalinan", "Pollux", "Regulus","Deneb", "Denebola");
      /* add a ScrollableList, by default it behaves like a DropdownList */
    //  Name1 = cp5.addScrollableList("StarName  1")
        Name1 = cp5.addDropdownList("StarName  1")
         .setPosition(20, 270)
         .setSize(300, 200)
         .setBarHeight(50)
         .setItemHeight(50)
         .addItems(l)
         // .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
         ;
    
    
         controlP5.addSlider("Latitude_1",0,90,Latitude_1,20,10,255,30);
         controlP5.addSlider("Lat_minutes",0,60,Lat_minutes,20,50,255,30);
         controlP5.addSlider("Lat_seconds",0,60,Lat_seconds,20,90,255,30);
    
         controlP5.addSlider("Longitude_1",-180,180,Longitude_1,20,130,255,30);
         controlP5.addSlider("Lon_minutes",0,60,Lon_minutes,20,170,255,30);
         controlP5.addSlider("Lon_seconds",0,60,Lon_seconds,20,210,255,30);
    
    
           Name2 = cp5.addScrollableList("StarName  2")
         .setPosition(20, 590)
         .setSize(300, 200)
         .setBarHeight(50)
         .setItemHeight(50)
         .addItems(l)
        ; 
    
    
    
    
    
        myTextarea = cp5.addTextarea("txt")
                      .setPosition(420,10)
                      .setSize(950, 720)
                      .setFont(createFont("arial", 20))
                      .setLineHeight(20)
                      .setColor(color(255))
                      .setColorBackground(color(255, 0))
                      .setColorForeground(color(255, 100));
    
        timer = millis();//store the current time
    
    
    }
    
    public void draw() {
     background(0, 0, 0);
        String[] starLines = loadStrings("stardata.csv");
        println("Loaded " + starLines.length + " stars:");
    
        Lat = (float)Latitude_1 + (float)Lat_minutes/60 + (float)Lat_seconds/3600;  //Estimated Latitude
        Lon = (float)Longitude_1 - (float)Lon_minutes/60 - (float)Lon_seconds/3600; //Estimated Longitude
    
       if (Longitude_1 > 0) {
       Lon = (float)Longitude_1 + (float)Lon_minutes/60 + (float)Lon_seconds/3600; //East of Greenwich
       }
    
        for ( i = 0 ; i < starLines.length; i++) {
    
            String[] stars = splitTokens(starLines[i], ", ");
            String proper = (stars[0]);
            String con = (stars[1]);
            double dec = PApplet.parseFloat(stars[2]);
            double ra = PApplet.parseFloat(stars[3]);
            double pmra = PApplet.parseFloat(stars[4]);
            double pmdec = PApplet.parseFloat(stars[5]);
            double rv = PApplet.parseFloat(stars[6]);
    
            ra = ra * 15;
    
            Star1 = Name1.getCaptionLabel().getText();
            if (Star1 == "Procyon") {
                z = 47;
            }
            if (Star1 == "Rasalhague") {
                z = 93;
            }
            if (Star1 == "Dubhe") {
                z = 57;
            }
            if (Star1 == "Mirphak") {
                z = 18;
            }
            if (Star1 == "Alkaid") {
                z = 73;
            }
    
    
            for (int j = 1; j < i; j = i+1) {
    
                try {
    
    
                        if (i == z) {   //14 = Polaris 69 = Dubhe
                            //  AstroDate astro = new AstroDate(2008, 4, 24, 10, 36, 18);
                            AstroDate astro = new AstroDate();
                            //  System.out.println(astro);
                            TimeElement time = new TimeElement(astro, SCALE.UNIVERSAL_TIME_UTC);
                            ObserverElement obs = new ObserverElement("", Lon * Constant.DEG_TO_RAD, Lat * Constant.DEG_TO_RAD, 88, 0);
                            ObserverElement obs0 = obs.clone();
                            EphemerisElement eph = new EphemerisElement(TARGET.Moon, COORDINATES_TYPE.APPARENT, EphemerisElement.EQUINOX_OF_DATE, EphemerisElement.GEOCENTRIC, EphemerisElement.REDUCTION_METHOD.IAU_2009,
                                    FRAME.DYNAMICAL_EQUINOX_J2000, EphemerisElement.ALGORITHM.MOSHIER); //EphemerisElement.ALGORITHM.MOSHIER); //EphemerisElement.ALGORITHM.JPL_DE405); //.ALGORITHM.JPL_DE405);
    
                            eph.correctForPolarMotion = false;   //originally false
                            eph.correctEOPForDiurnalSubdiurnalTides = false;  //originally false
                            eph.correctForEOP = true;  //originally false
                            //    EarthOrientationParameters.clearEOP();
                            Nutation.clearPreviousCalculation();
    
    
                            //    System.out.println("Geodetic location: "+Functions.formatAngleAsDegrees(obs.getLongitudeRad(), 3) + " / "+Functions.formatAngleAsDegrees(obs.getLatitudeRad(), 3) + " / "+obs.getHeight());
                            double TTminusUT1 = TimeScale.getTTminusUT1(time, obs);
                            // Force TT-UT1 as in the example
                            TTminusUT1 = 65.571845f;
                            TimeScale.forceTTminusUT1(time, obs, TTminusUT1);
                            // Force EOP parameters as in the example
                            double UT1minusUTC = -0.387845f, x = -0.002f, y = 0.529f, dx = 0, dy = 0;
                            boolean dxdyAredPsidEpsilon = false;
                            double jd_UTC = TimeScale.getJD(time, obs, eph, SCALE.UNIVERSAL_TIME_UTC);
                            //     EarthOrientationParameters.forceEOP(jd_UTC, eph, UT1minusUTC, x, y, dx, dy, dxdyAredPsidEpsilon);
    
                            double jd_TT = TimeScale.getJD(time, obs, eph, SCALE.TERRESTRIAL_TIME);
                            double jd_UT1 = TimeScale.getJD(time, obs, eph, SCALE.UNIVERSAL_TIME_UT1);
    
                            StarElement star = new StarElement(proper, (double) ra * Constant.DEG_TO_RAD, (double) dec * Constant.DEG_TO_RAD,
                                    0, 0, (float) (pmra * Constant.ARCSEC_TO_RAD / Math.cos((double) dec * Constant.DEG_TO_RAD)),
                                    (float) (pmdec * Constant.ARCSEC_TO_RAD), (float) rv, Constant.J2000, FRAME.ICRF);
                            //  System.out.println("Angular Velocity:  " + pmra);
                            StarEphemElement ephem = StarEphem.starEphemeris(time, obs, eph, star, false);
    
                            eph.isTopocentric = true;
                            ephem = StarEphem.starEphemeris(time, obs, eph, star, false);
                            //   double last = SiderealTime.greenwichMeanSiderealTime(time, obs, eph);
                            double last = SiderealTime.apparentSiderealTime(time, obs, eph);
                            // SiderealTime.
                            eph.correctForPolarMotion = false;  //was true
                            eph.correctEOPForDiurnalSubdiurnalTides = false; //was true
                            eph.correctForEOP = false; //was true
                            Nutation.clearPreviousCalculation();
                            ephem = StarEphem.starEphemeris(time, obs, eph, star, false);
                            // ephem = IAU2006.getStarEphemerisWithRespectCIO(time, obs, eph, star, CIO_EPHEMERIS.topocentric);
    
                            LocationElement loc = ephem.getEquatorialLocation();
    
                            System.out.println(loc);
    
    
                            boolean full_ephem = true;
                            EphemElement sephem = Ephem.getEphemeris(time, obs, eph, true);
                            // ConsoleReport.fullEphemReportToConsole(sephem);
                            String Results = ConsoleReport.getBasicEphemReport(sephem, false);
    
    
    
                            myTextarea.setText(("Geodetic location: " + Functions.formatAngleAsDegrees(obs.getLongitudeRad(), 3) + " / " + Functions.formatAngleAsDegrees(obs.getLatitudeRad(), 3) + " / " + obs.getHeight()) +
                                            "\n" + ("JD TT: " + jd_TT + "\n" + "UT1: " + jd_UT1 + "\n" + "TT-UT1: " + TTminusUT1) + "\n" +
                                            (proper + "  geocentric") + "\n" +
                                            ("RA:  " + Functions.formatValue(ephem.rightAscension * Constant.RAD_TO_DEG / 15.0f, 10)) + "\n" +
                                            ("RA:  " + Functions.formatRA(ephem.rightAscension, 4)) + "\n" +
                                            ("DEC: " + Functions.formatAngleAsDegrees(ephem.declination, 10)) + "\n" +
                                            ("DEC: " + Functions.formatDEC(ephem.declination, 4)) + "\n" + ("GP:  " + loc) + "\n" +
                                            ("Az:  " + Functions.formatValue(ephem.azimuth * Constant.RAD_TO_DEG, 10)) + "\n" +
                                            ("Az:  " + Functions.formatAngle(ephem.azimuth, 4)) + "\n" +
                                            ("Alt:  " + Functions.formatValue(ephem.elevation * Constant.RAD_TO_DEG, 10)) + "\n" +
                                            ("Alt:  " + Functions.formatDEC(ephem.elevation)) + "\n" + "\n" +
                                            //("Local Apparent Sidereal Time::  " + ( ((last)* Constant.RAD_TO_HOUR)+ 4)+"\n"+
                                            ("Local Apparent Sidereal Time::  " + (Functions.formatRA((last))) + "\n" + "\n" +
                                                    //  (Functions.formatRA ((last)))
                                                    ("Basic Ephemeris Report:  " + Results))
                                    //  ("Basic Ephem Report:  "))
                            );
                        }
                    }catch(Exception exc){
                        exc.printStackTrace();
                    }
    
                }
    
    
          //  }
        }
    
    
    
    
    }
    
    
    
    
      public void settings() {  size(1000, 750); }
      static public void main(String[] passedArgs) {
        String[] appletArgs = new String[] { "CSVTest2" };
        if (passedArgs != null) {
          PApplet.main(concat(appletArgs, passedArgs));
        } else {
          PApplet.main(appletArgs);
        }
      }
    }
    
  • @HarryCodes===

    You are perhaps right about dependencies; yet, looking to the code you have put and to the android demo code i think that .csv not found is the main problem: there are a lot of fiedls which ask for "star" and if this is undefined nothing can work; the other fiedls from android demo do not seem to make problems; as for the Observer you are right, this class has a lot of methods which return values from embedded data as . txt (list of cities, observatories, lat, lon....) but in the code of your applet you only call (for the observer) the clone() method which do not calculate any value, so i don't think that it is the problem you get now. Have you tried to import the android demo in AS??? - Does it work as-it-is???

  • Yup, processing android demo works fine with Android Studio -as-is. I just cant figure out how to effectively add jparsec to it.

  • If you drag the jar into your code in the PDE, then you should be able to access that library (Untested). This is how it works in Processing java and I would assume it is similar for Android mode. If you export the pde into AS, you should be able to see where the jar is located... I believe it will be in the libs folder.

    One thing: Do you need to open your csv file in draw()? If not, move it to setup().

    Regarding the assets, accessing files, dataPath(), etc. check this: https://forum.processing.org/two/discussion/comment/118947/#Comment_118947

    Kf

  • @HarryCodes, @kfrajer===

    -i think as kfrajer that it is not a good idea to get your csv in draw()

    • as for adding a .jar a with AS you have to create a libs folder, put here the .jar, go to your project view, right click on the . jar and add it as a library; then you go to your build.gradle and add it in the dependencies; finally you have to sync your project with gradle.
  • edited May 2018

    I got the @#$%^*&) to compile, install, run using Android Studio. The JPARSEC link I gave includes the Android example I mentioned above. I can get that to run all by itself. I snipped the libs and src directories from that and put them into this already imported processing for android project. The processing code I posted runs on Android now.

    All the advice you guys gave wasn't necessarily the solution but it still helped, thanks.

    What still does not work is adding the sketch to the android processing demo. The sketch will start but it cant read from various files so the textarea where the calculations are printed is blank. Android Studio shows the errors in logcat. It doesn't crash but doesn't read what it needs either.

Sign In or Register to comment.