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 › NullPointerException in nested loop
Page Index Toggle Pages: 1
NullPointerException in nested loop (Read 434 times)
NullPointerException in nested loop
Nov 26th, 2008, 9:16am
 
Hi All,

I'm trying to export some points to a text file within an ArrayList which is constantly being updated but the null pointer error comes up when I try to run the code. Apparently the problem occurs somewhere within the nested j loop, could it be a case of any arrays not being initialized correctly? I tried running the println() commands on the array size and they return values so they should not be empty.
Heres the code:

void exportToTxtStr() {  

 output1 = createWriter("lineExportStr.txt");

String pointList = "";
 for (int i = 0; i < world1.population.size(); i++) {
   kAgent a = (kAgent) world1.population.get(i);

   // THIS SECTION NOT WORKING
    for (int j = 0; j < a.posArrStr.size(); j++) {
      println(a.posArrStr.size()); //lookup array size
      kVec posVec = (kVec) a.posArrStr.get(j);
      if(j == 0){
          pointList = posVec.x + "," + posVec.y + "," + posVec.z;
          output1.println(pointList);
      }else{
          pointList = pointList + " " + posVec.x + "," + posVec.y + "," + posVec.z;
          output1.println(pointList);
           }
    }

 output1.flush();
 output1.close();

 }
}

And heres the error:

Exception in thread "Animation Thread" java.lang.NullPointerException

at kAgent_111.exportToTxtStr(kAgent_111.java:209)

at kAgent_111.keyPressed(kAgent_111.java:161)

at processing.core.PApplet.handleKeyEvent(PApplet.java:1724)

at processing.core.PApplet.dequeueKeyEvents(PApplet.java:1707)

at processing.core.PApplet.handleDraw(PApplet.java:1412)

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

at java.lang.Thread.run(Thread.java:595)

Any help would be appreciated!

Ben
Re: NullPointerException in nested loop
Reply #1 - Nov 26th, 2008, 10:00am
 
You are probably getting a null pointer here:

kAgent a = (kAgent) world1.population.get(i);

So, see if you can do this and find where you are getting that error:

void exportToTxtStr() {  

 output1 = createWriter("lineExportStr.txt");  

String pointList = "";
 for (int i = 0; i < world1.population.size(); i++) {
   kAgent a = (kAgent) world1.population.get(i);

   // THIS SECTION NOT WORKING

   if (a == null){
      println("####Detected null pointer at index: " + i);
   }else{
    for (int j = 0; j < a.posArrStr.size(); j++) {
 println(a.posArrStr.size()); //lookup array size
 kVec posVec = (kVec) a.posArrStr.get(j);
 if(j == 0){
     pointList = posVec.x + "," + posVec.y + "," + posVec.z;
     output1.println(pointList);
 }else{
     pointList = pointList + " " + posVec.x + "," + posVec.y + "," + posVec.z;
     output1.println(pointList);
 }
    }
}
 output1.flush();
 output1.close();

 }
}



The text output may give you some insight to what's happening.
Re: NullPointerException in nested loop
Reply #2 - Nov 26th, 2008, 10:31am
 
Hi sw01,

Thanks for the quick response. I did the check as per your suggestion: the line didn't show up so the array was filled there. I also tried the same check with the inner j loop (is the convention correct? Please look below) and no println regarding the index showed up either, so both of these arrays are passing something. I'm still clueless at why the error showed up.
Here is the modified code:

String pointList = "";
 for (int i = 0; i < world1.population.size(); i++) {
   kAgent a = (kAgent) world1.population.get(i);
    if (a == null){
 println("####Detected null pointer at index: " + i);
   }else{
    for (int j = 0; j < a.posArrStr.size(); j++) {
      println(a.posArrStr.size()); //lookup array size
      kVec posVec = (kVec) a.posArrStr.get(j);
      if (posVec == null){
        println("####Detected null pointer at index: " + j);
      }else{
      if(j == 0){
          pointList = posVec.x + "," + posVec.y + "," + posVec.z;
          output1.println(pointList);
      }else{
          pointList = pointList + " " + posVec.x + "," + posVec.y + "," + posVec.z;
          output1.println(pointList);
           }
    }
   }

output1.flush();
output1.close();

 }
}
}

Many thanks,

Ben
Re: NullPointerException in nested loop
Reply #3 - Nov 26th, 2008, 6:23pm
 
I'm sorry to hear that.

Is this using thread? i.e., Is wolrd1.population constant during the loop? If it is threaded, perhaps:
1) the object retrieved by .get(i) may not be available, or
2) the object retrieved by .get( ) statement is no longer available when it is looked up by other statements.

But I'm only guessing right now as I'm not entirely aware of how world1 is implemented, other than the fact that population is probably an ArrayList.

The j loop check is right.  I suppose a proper debugger would help in this type of situation...
Re: NullPointerException in nested loop
Reply #4 - Nov 26th, 2008, 11:28pm
 
Thanks for the tips. Upon closer inspection of the output by the console the error pointed to the line

for (int j = 0; j < a.posArrStr.size(); j++) {

so I think that there might be issues with either:

- the ArrayList population which is storing a series of objects (a). The population information is stored in a class and each frame there is a function which updates the objects in the population every frame.
Can you please elaborate on the issue with the .get method? I've simplified the code slightly so that the population stays constant after it is generated (in the setup() function)
- posArrStr which is storing yet another set of objects (which is a vector with 3 points)
- how information is passed between the i and j loops.

I have a feeling that it is an object-type conflict which is causing null pointers though. I'll keep trying to debug this. Do you know of any script or debugging resource that is more 'user-friendly' than the console? Please let me know!

Ben
Re: NullPointerException in nested loop
Reply #5 - Nov 27th, 2008, 1:44am
 
That information helps a lot: this makes it very likely to point to


"- posArrStr which is storing yet another set of objects (which is a vector with 3 points) "


If the code is throwing a null pointer error in that line, then a.posArrStr may be null. So, while "a" object is instantiated, its member posArrStr is not.  Try changing  

    if (a == null){
 println("####Detected null pointer at index: " + i);
   }else{  

to

    if (a.posArrStr == null){
 println("####Detected null pointer at index: " + i);
   }else{  


If this shows anything, the problem boils down to the constructor of kAgent object "a".  The default value of an object variable is a null pointer.  It is generally recommended that when a member is declared, its default value is explicitly stated, so perhaps in kAgent class definition,

ArrayList posArrStr = new ArrayList();

Should help alleviate this.  But this is my speculation only.


####
As far as an alternative to PDE console, Eclipse is recommended as a more full featured development environment.  I love using it, but I would not call it user-friendly.  As long as your project isn't too big or complicated, PDE should suffice for most experimentation, IMHO.
Re: NullPointerException in nested loop
Reply #6 - Nov 27th, 2008, 1:49am
 
I forgot to address the .get() issue.  

Don't worry about threading, as you mentioned that your "population" object remains unchanged during this routine.
Re: NullPointerException in nested loop
Reply #7 - Nov 27th, 2008, 8:57am
 
Hi there,
Problem solved - my instructor fixed it in 5 min :(

You were correct - it was something in the world1.population ArrayList. I actually had 2 different separate ArrayLists running (I had 2 subclasses with 2 different types of agents) so the function cannot determine which type of information to extract - hence the NullPointer error.
I had an identifier for the different Agent types and adding a conditional line to extract from the right type of ArrayList fixed the problem. Here is the modified code:

void exportToTxtStr() {  

 output1 = createWriter("lineExportStr.txt");
 
 String pointList = "";
 
 for (int i = 0; i < world1.population.size(); i++) {
   kAgent a = (kAgent) world1.population.get(i);

   if(a.agentType == "kAgentStr"){ // filters out the right object type!

    for (int j = 0; j < a.posArrStr.size(); j++) {
      kVec posVec = (kVec) a.posArrStr.get(j);
      if(j == 0){
          pointList = posVec.x + "," + posVec.y + "," + posVec.z;

      }else{
          pointList = pointList + " " + posVec.x + "," + posVec.y + "," + posVec.z;
           }
   }
   output1.println(pointList);
   }

 }
 
 output1.flush();
 output1.close();
}

Thanks for the help!
Happy Thanksgiving!

Ben
Re: NullPointerException in nested loop
Reply #8 - Nov 27th, 2008, 9:42am
 
Good to know. I'm kind of glad that the problem was a bit out of the scope of this code.  

I was worried that something really obvious was missing and making a fool of myself Wink

Page Index Toggle Pages: 1