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 › problem with nested for loop
Page Index Toggle Pages: 1
problem with nested for loop (Read 565 times)
problem with nested for loop
Oct 15th, 2008, 9:07pm
 
Hi there,

I am having difficulty running this code and can't figure out why. The fourth nested loop doesn't seem to want to iterate. Also, I get the following null pointer exception:

java.lang.NullPointerException

at processing.core.PApplet.displayable(PApplet.java:8345)

at processing.app.Runner.startInternal(Runner.java:351)

at processing.app.Runner.start(Runner.java:79)

at processing.app.Editor.handleRun(Editor.java:1112)

at processing.app.EditorToolbar.mousePressed(EditorToolbar.java:316)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Which I don't really understand. Here is the code that I used..... Is the syntax ok? Why is it crashing? Any ideas?

final int TOTINV = 4;
final int PERMS = 35;
int[][] vec = new int[PERMS][TOTINV];
int counter;
boolean initted = false;
PrintWriter output;

void setup(){
output = createWriter("perms.txt");
for(int x = 0; x < PERMS; x++){
 for(int y = 0; y < TOTINV; y++){
   vec[x][y]=0;
 }
}
}

void main(){
 if (!initted){
   init();
   initted = true;
 }else{
   output.flush();
   output.close();
   println("finished");
   exit();
 }
}

void init(){
 counter = 0;
 for(int i = 0; i < TOTINV+1; i++){
   for(int j = 0; j < TOTINV+1; j++){
     for(int k = 0; k < TOTINV+1; k++){
       for(int m = 0; m < TOTINV+1; m++){
         println(i + ":" + j + ":" + k + ":" + m);
         if((i+j+k+m) == 4){
           vec[counter][0]= i;
           vec[counter][1]= j;
           vec[counter][2]= k;
           vec[counter][3]= m;
           println(counter +": " + vec[counter][0] + ", " + vec[counter][1] + ", " + vec[counter][2] + ", " + vec[counter][3]);
           //output.println(counter +": " + vec[counter][0] + ", " + vec[counter][1] + ", " + vec[counter][2] + ", " + vec[counter][3]);
           counter++;
         }else{
           break;
         }
       }
     }
   }
 }
}
Re: problem with nested for loop
Reply #1 - Oct 15th, 2008, 10:45pm
 
init() is a built-in function, so you can't have a function of that name.
Re: problem with nested for loop
Reply #2 - Oct 16th, 2008, 1:16pm
 
Thank you Ben. Changing the names of init() and main() got rid of the null pointer exception and the program runs now. However I am still puzzled why the program doesn't seem to iterate through my innermost for loop. Any ideas why anyone?

Re: problem with nested for loop
Reply #3 - Oct 16th, 2008, 1:19pm
 
Ok, problem solved, I just needed to take out the else statement and it's all fixed now. Thanks again.
Page Index Toggle Pages: 1