Loading...
Logo
Processing Forum
I am creating a sketch which is capable of publishing a scatter plot and parallel plot on two different pages. Both the sketches work perfectly when I run them individually but when I run them as a single sketch with two page then one sketch works but the other generates an error message. The code is provided below and the source of the error is highlighted in red. How do I resolve the NullPointerException error? 

Copy code
  1. import org.gicentre.utils.colour.*;  // For colour tables.


  2. Table irisData;        // All the Iris data.
  3. float[] colours;       // Line colours.
  4. float[] maxValues;     // Maximum data values for each column.
  5. ColourTable cTable;    // Line colour table.

  6. void setup2()
  7. {
  8.   size(600,600);
  9.   smooth();
  10.   textAlign(CENTER,CENTER);
  11.   textSize(18);
  12.   readData();
  13. }

  14. void drawPage1()
  15. {
  16.   background(255);
  17.   // The x-position of the two parallel axes.
  18.   float x1 = width*.25;
  19.   float x2 = width*.75;
  20.    
  21.   // Plot the data items.
  22.   strokeWeight(0.7);
  23.   for (int row=0; row<irisData.getRowCount(); row++)
  24.   {
  25.     // Sepal length in column 1, petal length in column 3.
  26.     float sLength = irisData.getFloat(row,1);
  27.     float pLength = irisData.getFloat(row,3);
  28.     
  29.     // Scale to screen coordinates.
  30.     float y1 = map(sLength,0,maxValues[0],height-10,10);
  31.     float y2 = map(pLength,0,maxValues[2],height-10,10);
  32.     
  33.     // Draw lines.
  34.     stroke(cTable.findColour(colours[row]));
  35.     line(x1,y1,x2,y2);
  36.   }
  37.   
  38.    // Plot the parallel axes.
  39.   stroke(50);
  40.   strokeWeight(2);
  41.   line(x1,10,x1,height-10);
  42.   line(x2,10,x2,height-10);
  43.   
  44.   fill(120);
  45.   float textHeight = textAscent()+textDescent();
  46.   verticalText("Per capita recorded alcohol consumption - adults",x1-textHeight,height/2);
  47.   verticalText("% who have been drunk at least twice, aged <15",x2+textHeight,height/2);
  48. }

  49. // Displays text vertically.
  50. void verticalText(String label, float x, float y)
  51. {
  52.   pushMatrix();
  53.    translate(x,y);
  54.    rotate(radians(-90));  
  55.    text(label,0,0);
  56.   popMatrix();
  57. }


Replies(4)

If you have an NPE there, it means you haven't initialized correctly irisData, it is still null.
Thanks for responding phil.lho. A quick question, does that mean that the irisData.tsv is currupted?
When I run this sketch as a separate sketch the sketch works fine but I only seem to have a problem when I try to fuse this sketch with another sketch.
Could you please advise me on a possible solution that could rectify the problem?
I fear I can't answer your question, because I see nowhere how irisData is created. In the code you show, it just remains uninitialized.
Thanks for taking your time to reply. I managed to trouble shoot it so its all good.