Empty an array, NullPointerException

edited January 2015 in Questions about Code

Newbie to processing, using Arduino IDE for 18 months.
Win7, Processing 2.2.1

My Arduino central heat controller sends a serial string of data once per minute. My laptop, running a processing sketch, receives this string, appends it to a string array, and every so often saves the array to a file on disk (the objective is to save it once a day, which is just under 1500 lines, but in the test version I'm saving the file with only 4 lines).

My problem is that once the array is saved, I need to "empty" it. Otherwise, the code dataArray = append(dataArray, dataLine); will continue to grow the array, rather than start over refilling it. I say "empty" because I don't need to collapse the array and recover memory, I just need the array pointer to be reset to the first line. In the normal course of events, all the data in the array would be overwritten with new data. Each day the array will be similar in size (plus or minus one or two lines) and any extraneous line will be obvious because each line is time tagged. The data is eventually plotted in Excel. Here's the full code:

import processing.serial.*;
int n;
int today ;
int yesterday ;
String filename ;
String dataLine = "" ;
String[] dataArray = {};
Serial dataPort ;
// ----------------------------------------------------
void setup() {
  // Open whatever port you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  dataPort = new Serial(this, portName, 9600); 
  n = 0 ;
  //  filename = nf(year(),4,0)+nf(month(),2,0)+nf(day(),2,0)+'_'+nf(hour(),2,0)+nf(minute(),2,0)+".CSV" ;
  //   println(filename) ;
  println(Serial.list()) ;
  yesterday = day() ;
  }  
// -----------------------------------------------------  
void draw() {
  if ( dataPort.available() > 0) {  // If data is available
    n=n+1 ;
    dataLine = dataPort.readStringUntil('\n');  // read it and store it in dataLine
    if(dataLine != null) {
     dataLine = trim(dataLine) ;  // remove carriage return at end of line
     dataLine = nf(hour(),2,0)+':'+nf(minute(),2,0)+':'+nf(second(),2,0)+','+dataLine ;
     dataArray = append(dataArray, dataLine);
     println(dataLine); //print it out in the console
    } }
  today = day() ; 
  //if(today != yesterday) {
    if(n>3) {  // the loop counter "n" is used only for testing
    println("saving to file") ;
    filename = nf(year(),2,0)+nf(month(),2,0)+nf(yesterday,2,0)+"_"+nf(second(),2,0)+".CSV" ;     
    println(filename) ;
    yesterday = today ;
    saveStrings(filename, dataArray) ; 
    dataArray = null ;
    dataArray[0] = "" ;
    n=0 ; }
  delay(6000) ;
}

The output in the console is as expected the first time through:

COM41
23:07:29,11,1,65.97,65.64,0.00,0.00,66.20,65.75,65.97,65.64
23:08:29,01,1,66.09,65.64,0.00,0.00,66.31,65.86,65.86,65.75
23:09:35,61,1,66.09,65.64,0.00,0.00,66.31,65.75,65.97,65.64
23:10:29,41,1,65.97,65.64,0.00,0.00,66.20,65.75,65.97,65.64
saving to file
20150112_29.CSV

I've used these lines from other forum recommendations to try and empty the dataArray, but I get the NullPointerException either in the second line or elsewhere in the sketch:

dataArray = null ;
dataArray[0] = "" ;

Thanks for any insight. Dr Quark

Answers

  • you don't need line 2

    in line 1 you are removing the reference associated with dataArray. on line 2 you are then trying to use the reference you've just destroyed. that just won't work.

  • I gathered that from one of the posts I read. Actually, the post said that dataArray = ""was a way to erase the array without filling it with zeros, but also indicated that doing so would not reset the array pointer. I'll comment out line 1 and see what happens.

  • BTW, if I comment out line 2, I get the NullPointerException in line 28 in the big block of code.

  • Answer ✓

    Try replacing lines 39 and 40 with

    dataArray = new String [0];

    This will create an array with zero elements but hopefully allow you to append new strings in line 28

  • edited January 2015 Answer ✓

    sorry, i read those last two lines in isolation. if you're going to continue using dataArray then nulling it will cause problems elsewhere, yes (unless you recreate it)

    however, i would say to use an ArrayList rather than an array and append() - arraylist is designed to be extendable, append is an ugly hack. but... http://forum.processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append

    (there was a FAQ for this in the wiki, it got removed somewhere along the line)

    ok, some discussion here: https://processing.org/discourse/beta/num_1230887778.html

  • OK, commenting out line 1(line 39 in the full code), results in the second saved file having a blank first line, then 3 lines from the first save, followed by 4 lines from the second save.

    How do I get the append() operation (line 28) to restart at line/element zero (0)?

    Dr_ Quark

  • see quark's answer

  • Thanks, Koogs. I read a few of the posts on Arraylists, but took the shortcut of append(). I'll take another look. BTW, my post immediately above was being written while you posted.

  • edited January 2015

    Quark, your suggestion worked great, as far as I can tell by looking at two cycles.

        filename = "C:/00--Public/Data/"+filename;
        println(filename) ;
        saveStrings(filename, dataArray) ; 
        dataArray = new String [0] ;
        n=0 ; }
    

    Your code is at line 4. The second file save had only four lines in the file and they were all new. Thanks.

    BTW, koogs, your first link goes to a post that has no body, only the header and title. The second link works fine. I'll dive into Arraylists later (lazy and under time pressure).

    Dr Quark

  • BTW, koogs, your first link goes to a post that has no body, only the header and title

    i know, hence my comment in brackets.

    i had a look for it on the internet wayback machine but it was empty there as well. but the other link will do.

Sign In or Register to comment.