We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › sort() for String[]
Page Index Toggle Pages: 1
sort() for String[] (Read 1302 times)
sort() for String[]
Oct 6th, 2007, 3:03am
 
I'm trying to sort a string array,
but I get the following error when sort() is called:

java.lang.NullPointerException
at processing.core.PApplet.sort_compare(PApplet.java:4934)

Can you see if something is wrong in my code ?

int imax=4;
String[] seq = new String[imax+1];
String[] seqSorted = new String[imax+1];

void setup() {
 size(100,100);
 noLoop();
}

void draw() {
 //
 println("A -------> string array is empty");
 for (int i=0; i<imax; i++) {
   println(seq[i]);
 }
 //
 float theta=1.45;
 int index=0;
 for (int c=0; c<imax; c++) {
     seq[index]=nf(c*theta%1,3,5)+" "+str(c);
     index++;
 }
 //
 println("B -------> string array is filled");
 for (int i=0; i<imax; i++) {
   println(seq[i]);
 }
 //
 seqSorted = sort(seq);// <========= runtime ERROR here
 for (int i=0; i<imax; i++) {
   println(seqSorted[i]);
 }
}
Re: sort() for String[]
Reply #1 - Oct 6th, 2007, 3:56am
 
Your array is larger than you need.  The extra place is null and this is why you get the null pointer error.
instead:
Code:

int imax=4;
String[] seq = new String[imax];
String[] seqSorted = new String[imax];

void setup() {
size(100,100);
noLoop();
}

void draw() {
//
println("A -------> string array is empty");
for (int i=0; i<imax; i++) {
println(seq[i]);
}
//
float theta=1.45;
int index=0;
for (int c=0; c<imax; c++) {
seq[index]=nf(c*theta%1,3,5)+" "+str(c);
index++;
}
//
println("B -------> string array is filled");
for (int i=0; i<seq.length; i++) {
println(seq[i]);
}
//
seqSorted = sort(seq);// <========= runtime ERROR here
for (int i=0; i<imax; i++) {
println(seqSorted[i]);
}
}


Re: sort() for String[]
Reply #2 - Oct 6th, 2007, 6:12am
 
OK, Thanks (that was an unexpected error!)

Here is some explanation of what I'm trying to do:

In this program segment, I need to sort an array of float values, while keeping track of the index permutation.
So sorting {1.2, 0.7, 3.2} to {0.7, 1.2, 3.2},
has index going {0, 1, 2} to {1, 0, 2}.

To use the sort() function,
I concatenate Value and Index,
going {"1.2 0", "0.7 1", "3.2 2"} to {"0.7 1", "1.2 0", "3.2 2"}

Then I still need to extract the numeric values from the string.

Repeating the code:

int imax=4;
float[] seqFloat = new float[imax];
String[] seqString = new String[imax];
String[] seqStringSorted = new String[imax];

void setup() {
 size(100,100);
 noLoop();
}

void draw() {
 //
 //setup initial float array
 float theta=1.45;
 int index=0;
 for (int c=0; c<imax; c++) {
     seqFloat[index]=c*theta%1;
     index++;
 }
 //
 println("A -------> initial float array");
 for (int i=0; i<imax; i++)
   println(seqFloat[i]);
 //
 //setup string array
 for (int i=0; i<imax; i++) {
     seqString[i]=nf(seqFloat[i],3,5)+" "+str(i);
     index++;
 }
 //
 println("B -------> initial string array");
 for (int i=0; i<imax; i++)
   println(seqString[i]);
 //
 seqStringSorted = sort(seqString);
 println("C -------> sorted string array");
 for (int i=0; i<imax; i++)
   println(seqStringSorted[i]);
}
Page Index Toggle Pages: 1