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 & HelpSyntax Questions › problem with expand in array
Page Index Toggle Pages: 1
problem with expand in array (Read 469 times)
problem with expand in array
Aug 10th, 2006, 12:51am
 
I try to add data into an array of share class and I have an error with expand to increase one position lenght more in teh array.  
Code:

share[] myShares = {};
int total = 0;

void setup(){
Add("BBVA","es");
Add("AZN","us");
}
void Add(String value1, String value2){
if ( myShares != null){
if (total == myShares.length){
total++;
myShares = expand( myShares,total);
myShares[total-1].name = value1;
myShares[total-1].market = value2;
}
}
}
class share{
String name;
String market;
}

that is the error:
Code:

C:/DOCUME~1/MARCAN~1/CONFIG~1/Temp/build31456.tmp/Temporary_3458_459.java:12:8:12:41: Semantic Error: The type of the right sub-expression, "java.lang.Object", is not assignable to the variable, of type "Temporary_3458_459$share[]".


Maybe someone know the problem of that. Thanks.

-- Mar Canet
mcanet (at) prof.esdi.es
Re: problem with expand in array
Reply #1 - Aug 10th, 2006, 9:26am
 
If you take a lok at the reference for expand you will see that it only works with boolean, byte, chars, int, float, or String arrays. But you have an share array. I think it would be easier for to work with vectors, cause they will expand automaticly: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html.
Re: problem with expand in array
Reply #2 - Aug 10th, 2006, 4:17pm
 
actually it works with all types now, as of more recent releases (see revisions.txt, the ref is out of date).

the problem is that an "Object" is returned and needs to be type cast so that it's clear to the compiler that you know what you're doing:

myShares = (share[]) expand( myShares,total);
Re: problem with expand in array
Reply #3 - Aug 11th, 2006, 1:33am
 
Thanks Fry!! Now I don't have problems with expand, but the error is now in when save the value in the object.

Code:

read_data reader1;
share[] myShares = {};
int total = 0;
void setup(){
AddMyGui_array("BBVA","es");
AddMyGui_array("AZN","us");
}
void AddMyGui_array(String value1, String value2){
if ( myShares != null){
if (total == myShares.length){
total++;
myShares = (share[]) expand( myShares,total);
//myShares = expand( myShares,total);
myShares[total-1].name = value1;
myShares[total-1].market = value2;
}
}
}
class share{
String name;
String market;
}


That is the error.
Code:

java.lang.NullPointerException

at Temporary_828_829.AddMyGui_array(Temporary_828_829.java:26)

at Temporary_828_829.setup(Temporary_828_829.java:5)

at processing.core.PApplet.handleDisplay(PApplet.java:1269)

at processing.core.PGraphics.requestDisplay(PGraphics.java:535)

at processing.core.PApplet.run(PApplet.java:1152)

at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException


at Temporary_828_829.AddMyGui_array(Temporary_828_829.java:26)


at Temporary_828_829.setup(Temporary_828_829.java:5)


at processing.core.PApplet.handleDisplay(PApplet.java:1269)


at processing.core.PGraphics.requestDisplay(PGraphics.java:535)


at processing.core.PApplet.run(PApplet.java:1152)


at java.lang.Thread.run(Unknown Source)

Thanks
Re: problem with expand in array
Reply #4 - Aug 11th, 2006, 6:22pm
 
that's because you're not creating a new share object. try this:

Code:

void AddMyGui_array(String value1, String value2){
if ( myShares != null) {
if (total == myShares.length){
// don't expand to 'total', just double its size--more efficient
myShares = (share[]) expand( myShares);
}
share newbie = new share(value1, value2);
myShares[total] = newbie;
total++;
}
}

class share{
String name;
String market;

public share(String name, String market) {
this.name = name;
this.market = market;
}
}

then if you're thinking you're hot s--, you can even condense those last lines to change:

 share newbie = new share(value1, value2);
 myShares[total] = newbie;
 total++;

into this:

myShares[total++] = new share(value1, value2);

which will do the same thing.
Re: problem with expand in array
Reply #5 - Aug 11th, 2006, 9:48pm
 
Now is ok!!
Only I have to change a line of code when I define the share array.

share[] myShares = {};

to

share[] myShares = new share[1];

Thanks a lot Fry.

Code:

share[] myShares = new share[1];
int total = 0;
void setup(){


add_value_to_array("BBVA","es");
add_value_to_array("AZN","us");
for(int i=0;i<myShares.length;i++){
println("i:"+i);
println(myShares[i].name+"::"+myShares[i].market);
}
}
void add_value_to_array(String value1, String value2){

if(total == myShares.length){
myShares =(share[]) expand(myShares,(myShares.length+1));
}

share newbie = new share(value1,value2);
myShares[total++] = newbie;
//total++;
}
class share{
public String name;
public String market;
public share(String name, String market) {
this.name = name;
this.market = market;
}
}
Page Index Toggle Pages: 1