Resolving NullPointerException
in
Programming Questions
•
2 years ago
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?
- import org.gicentre.utils.colour.*; // For colour tables.
- Table irisData; // All the Iris data.
- float[] colours; // Line colours.
- float[] maxValues; // Maximum data values for each column.
- ColourTable cTable; // Line colour table.
- void setup2()
- {
- size(600,600);
- smooth();
- textAlign(CENTER,CENTER);
- textSize(18);
- readData();
- }
- void drawPage1()
- {
- background(255);
- // The x-position of the two parallel axes.
- float x1 = width*.25;
- float x2 = width*.75;
- // Plot the data items.
- strokeWeight(0.7);
- for (int row=0; row<irisData.getRowCount(); row++)
- {
- // Sepal length in column 1, petal length in column 3.
- float sLength = irisData.getFloat(row,1);
- float pLength = irisData.getFloat(row,3);
- // Scale to screen coordinates.
- float y1 = map(sLength,0,maxValues[0],height-10,10);
- float y2 = map(pLength,0,maxValues[2],height-10,10);
- // Draw lines.
- stroke(cTable.findColour(colours[row]));
- line(x1,y1,x2,y2);
- }
- // Plot the parallel axes.
- stroke(50);
- strokeWeight(2);
- line(x1,10,x1,height-10);
- line(x2,10,x2,height-10);
- fill(120);
- float textHeight = textAscent()+textDescent();
- verticalText("Per capita recorded alcohol consumption - adults",x1-textHeight,height/2);
- verticalText("% who have been drunk at least twice, aged <15",x2+textHeight,height/2);
- }
- // Displays text vertically.
- void verticalText(String label, float x, float y)
- {
- pushMatrix();
- translate(x,y);
- rotate(radians(-90));
- text(label,0,0);
- popMatrix();
- }
1