|
Null pointer exception error
Hi,
I am getting a null pointer exception error for the line:
tester.Results("test.data");
in the following code:
public class Project2 {
final static int HistogramSize=10;
public int segmentCounter;
public int passOriginCounter;
public double maxlength=0.0;
public double minlength=0.0;
public double maxangle=0.0;
public double minangle=0.0;
public double [] distribution = new double[HistogramSize];
private int intersectCounter;
public Project2() {
// constructor for the class
}
public void Results(String FileName){
MaInput F1 = new MaInput("test.data");
double Ax=0.0, Ay=0.0, Bx=0.0, By=0.0;
while (!F1.atEOF()) {
Ax = F1.readDouble();
Ay = F1.readDouble();
Bx = F1.readDouble();
By = F1.readDouble();
if ( Math.abs(Ax - Bx) >= Point.GEOMTOL && Math.abs(Ay - By) >= Point.GEOMTOL) {
segmentCounter++;
LineSegment L = new LineSegment(Ax, Ay, Bx, By);
if (L.passOrigin()) passOriginCounter++;
if (L.segmentLength() - maxlength >= Point.GEOMTOL)
maxlength = L.segmentLength();
if (L.segmentLength() - minlength <= Point.GEOMTOL)
minlength = L.segmentLength();
if (L.segmentAngle() - maxangle >= Point.GEOMTOL)
maxangle = L.segmentAngle();
if (L.segmentAngle() - minangle <= Point.GEOMTOL)
minangle = L.segmentAngle();
if (L.segmentAngle() >= 0.0 && L.segmentAngle() <= 18.0) distribution[0]++;
if (L.segmentAngle() > 18.0 && L.segmentAngle() <= 36.0) distribution[1]++;
if (L.segmentAngle() > 36.0 && L.segmentAngle() <= 54.0) distribution[2]++;
if (L.segmentAngle() > 54.0 && L.segmentAngle() <= 72.0) distribution[3]++;
if (L.segmentAngle() > 72.0 && L.segmentAngle() <= 90.0) distribution[4]++;
if (L.segmentAngle() > 90.0 && L.segmentAngle() <= 108.0) distribution[5]++;
if (L.segmentAngle() > 108.0 && L.segmentAngle() <= 126.0) distribution[6]++;
if (L.segmentAngle() > 126.0 && L.segmentAngle() <= 144.0) distribution[7]++;
if (L.segmentAngle() > 144.0 && L.segmentAngle() <= 162.0) distribution[8]++;
if (L.segmentAngle() > 162.0 && L.segmentAngle() <= 180.0) distribution[9]++;
}
}
for ( int a=0; a<=9; a++)
distribution[a] = distribution[a]/segmentCounter;
LineSegment [] segments = new LineSegment [segmentCounter];
while (!F1.atEOF()) {
for ( int i=0; i<segmentCounter; i++ ) {
if ( Math.abs(Ax - Bx) > Point.GEOMTOL && Math.abs(Ay - By) > Point.GEOMTOL)
segments[i] = new LineSegment( F1.readDouble(), F1.readDouble(), F1.readDouble(), F1.readDouble() );
}
}
for ( int n=0; n<segmentCounter; n++) {
for ( int m=n+1; m<=segmentCounter; m++) {
if (segments[n].intersectSegment(segments[m])) intersectCounter++;
}
}
}
public int getNumberIntersectingSegments(){
return intersectCounter;
}
public static void main(String args[]) {
// You should enter your own tester of the methods in this
// class here
Project2 tester = new Project2();
tester.Results("test.data");
int nointersects = tester.getNumberIntersectingSegments();
System.out.println("Information about the segments:");
System.out.println(" Number of segments: " +tester.segmentCounter);
System.out.println(" Number of lines passing through zero: " +tester.passOriginCounter);
System.out.println(" Number of intersecting segments: " +nointersects);
System.out.println(" Max length: " +MaF.dF(tester.maxlength, 6, 2));
System.out.println(" Min length: " +MaF.dF(tester.minlength, 6, 2));
System.out.println(" Max angle: " +MaF.dF(tester.maxangle, 6, 2));
System.out.println(" Min angle: " +MaF.dF(tester.minangle, 6, 2));
System.out.println(" Angle (in deg) distribution:");
System.out.println(" [0.0, 18.0] : " +MaF.dF(tester.distribution[0], 5, 3));
System.out.println(" [18.0, 36.0] : " +MaF.dF(tester.distribution[1], 5, 3));
System.out.println(" [36.0, 54.0] : " +MaF.dF(tester.distribution[2], 5, 3));
System.out.println(" [54.0, 72.0] : " +MaF.dF(tester.distribution[3], 5, 3));
System.out.println(" [72.0, 90.0] : " +MaF.dF(tester.distribution[4], 5, 3));
System.out.println(" [90.0, 108.0] : " +MaF.dF(tester.distribution[5], 5, 3));
System.out.println(" [108.0, 126.0] : " +MaF.dF(tester.distribution[6], 5, 3));
System.out.println(" [126.0, 144.0] : " +MaF.dF(tester.distribution[7], 5, 3));
System.out.println(" [144.0, 162.0] : " +MaF.dF(tester.distribution[8], 5, 3));
System.out.println(" [162.0, 180.0] : " +MaF.dF(tester.distribution[9], 5, 3));
}
}
I think that the code I have written will do what I want it to, and am using the main method as a test for this. Perhaps I should mention that all of the methods called that do not appear in this code are present and working in other class files.
I have read some of the other posts regarding this error, but nothing that I try seems to work. I would be very grateful if someone could point out where I'm going wrong, and suggest how I can fix this.
Thanks in advance.
|