I have a csv file with rows of data, some of it numbers, some of it text. I'm trying to store this data in a String[][], access it later and then convert it into a useful format.
- Exception in thread "Animation Thread" java.lang.NullPointerException
- at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:989)
- at java.lang.Float.valueOf(Float.java:388)
- at java.lang.Float.<init>(Float.java:489)
- at processing.core.PApplet.parseFloat(PApplet.java:6566)
- at processing.core.PApplet.parseFloat(PApplet.java:6561)
- at GESUS$Planet.visualise(GESUS.java:526)
- at GESUS$Planet.plot(GESUS.java:364)
- at GESUS.draw(GESUS.java:105)
- at processing.core.PApplet.handleDraw(PApplet.java:1631)
- at processing.core.PApplet.run(PApplet.java:1530)
- at java.lang.Thread.run(Thread.java:680)
- // Welcome to GESUS
- // Geospatial Earth Simulation Using Sound (SuperCollider)
- import processing.opengl.*;
- import javax.media.opengl.*; //use this elsewhere, but removed that code.
- import tomc.gpx.*; //maybe there are conflicts with the libraries I am using?
- import java.util.Calendar;
- import java.util.TimeZone;
- import java.lang.Math;
- import java.io.File;
- GL gl;
- PGraphicsOpenGL pgl;
- //Equator equator; //class used for solving equations, used elswhere but code removed
- Planet earth;
- public GPX planetGeography;
- XMLElement godDirectory;
- void setup() {
- size(1600,1000,OPENGL);
- background(0);
- frameRate(300);
- earth = new Planet();
- getDataPaths();
- }
- void getDataPaths() {
- godDirectory = new XMLElement(this,"godDirectory.xml");
- int godCount = godDirectory.getChildCount();
- for(int i=0;i<godCount;i++) {
- XMLElement godLayer = godDirectory.getChild(i);
- GOD GODLayer = new GOD(godLayer);
- }
- }
- //This is GOD, the class for a Geospatially Overlaid Dataset.
- //It takes a data input, parses it and is then called by the Planet to which it is assigned.
- class GOD {
- String name;
- String source;
- String path;
- String filetype;
- String seperator;
- int fieldCount;
- HashMap metaValues;
- XMLElement metaData;
- String[][] godLayerData;
- ArrayList godLayer;
- GOD(XMLElement GODLayer) {
- metaData = GODLayer;
- getMetadata();
- loadData();
- passData();
- }
- void getMetadata() {
- name = metaData.getChild("name").getContent();
- source = metaData.getChild("source").getContent();
- path = metaData.getChild("path").getContent();
- filetype = metaData.getChild("filetype").getContent();
- seperator = metaData.getChild("seperator").getContent();
- fieldCount = metaData.getChild("values").getChildCount();
- godLayer = new ArrayList();
- metaValues = new HashMap(fieldCount+1,1);
- for (int i=0;i<fieldCount;i++) {
- metaValues.put(metaData.getChild("values").getChild(i).getContent(),i);
- }
- }
- void loadData() {
- if (filetype==null) {
- println("filetype for "+name+" is either not set or caught");
- }
- else if (filetype!=null) {
- if (filetype.equals("txt")) {
- String[] rawLines = loadStrings(path);
- String[][] godLayerData = new String[rawLines.length][fieldCount];
- for (int i=1;i<rawLines.length;i++) {
- String[] splitLine = split(rawLines[i], seperator);
- for (int j=0;j<metaValues.size();j++) {
- godLayerData[i][j] = splitLine[j];
- }
- }
- metaValues.put("datasetSize",rawLines.length-1);
- godLayer.add(metaValues);
- godLayer.add(godLayerData);
- }
- else if (filetype.equals("csv")) {
- String[] rawLines = loadStrings(path);
- String[][] godLayerData = new String[rawLines.length][fieldCount];
- for(int i=1;i<rawLines.length;i++) {
- String[] splitLine = split(rawLines[i], seperator);
- for(int j=0;j<fieldCount;j++) {
- godLayerData[i][j] = splitLine[j];
- }
- }
- metaValues.put("datasetSize",rawLines.length-1);
- godLayer.add(metaValues);
- godLayer.add(godLayerData);
- }
- else {
- println("filetype '"+filetype+"' is not recognised");
- }
- }
- }
- void passData() {
- earth.put(godLayer);
- }
- }
earth is an instance of Planet, below (there might be some irrelevant variables, I use them in code not included):
- class Planet {
- float pRadius;
- int pAspect;
- int pLongitudeLineIncrement;
- int pLatitudeLineIncrement;
- int pDepth;
- int pDarksideVisibility;
- String pGeographyFile;
- float pRotationVelocity;
- PVector pCentrePoint;
- float pSolarDeclination;
- ArrayList godInputs;
- Planet() {
- pRadius = 2.0*int(min(height, width)*0.4);
- pAspect = 0;
- pLongitudeLineIncrement = 15;
- pLatitudeLineIncrement = 15;
- pDepth = 1000;
- pCentrePoint = new PVector(width/2, height/2, -pDepth);
- pDarksideVisibility = 150; //set the opacity of the darkside of the Earth (0-255)
- pGeographyFile = "continents.gpx"; //GPX file containing the earth's geography
- godInputs = new ArrayList();
- }
- void put(ArrayList input) {
- if(input==null) {
- println("data input is empty");
- }
- else {
- godInputs.add(input);
- }
- }
- void visualise() {
- for(int i=0;i<godInputs.size();i++){
- ArrayList godLayer = (ArrayList) godInputs.get(i);
- HashMap godMetaData = (HashMap) godLayer.get(0);
- String[][] godLayerData = (String[][]) godLayer.get(1);
- int datasetSize = int(godMetaData.get("datasetSize").toString());
- for(int j=0;j<datasetSize;j++) {
- String latitude = godLayerData[j][int(godMetaData.get("latitude").toString())];
- println(latitude); //this works absolutely fine!
- //float latitudeFloat = float(latitude);
- //why can i not convert this string into a float???
- }
- }
- }
- }
The weirdest part is that if you run this code as is, it prints latitudes out no problem. However, as soon as you uncomment the "float latitudeFloat = float(latitude);" line, it prints NULL followed by the error!!! Huh? How can handling a variable LATER affect it earlier in the program? Am I making a dumb mistake or is there something sinister going on?
Thanks in advance for the help, and if there are any other tips on how to make this work better I'd love to hear them!
1