Results 1 to 4 of 4
  1. #1
    brownie_jedi is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    please include the stack trace .. i hate going through the code ..
    dont worry newbie, we got you covered.

  3. #3
    brownie_jedi is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    /**
     * Project 2 - Term II 2002
     *
     * @author    Mark Wallington (m.wallington@warwick.ac.uk)
     * student no: 0620929
     */
    
    /*************************************************************
     * 
     * This file is a template which should be modified to be
     * fully functional
     *
     * Follow the instructions below. The names of methods typed
     * explicitly in this class must be kept as defined.
     *
     *************************************************************/
    
    public class Project2 {
    
        // The names of these instance variables should not be modified
        // Enter comments which explain what results they store.
    	final static int HistogramSize=10;
    	public int segmentCounter=0;
    	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];
    
        // define a private variable that stores number of 
        // intersecting pairs
        
        	private int intersectCounter=0;
    	
    
        public Project2() {
    	// constructor for the class, does not need any modifications
        }
    
    
    
    
        /*****************************************************************
         * 
         * Results - this is the core methods which should perform
         *           all tasks required in the formulation of the project
         *
         * @param String FileName - the name of the input file
         *
         *****************************************************************
         */
    
        public void Results(String FileName){
    
    	// you have to implement the method here
            // it should read the data and then perform all tasks necessary
            // for computing (and storing in the instance variables) information
            // about the data set (as required by the formulation of the project)
        
    
    	/*--------------------------------------------------
    	 *  A suggested simple and straightforward  
    	 *  structure of the code here
    	 * 1/ read data from the file 
    	 *    say MaInput F1 = new MaInput(FileName);
    	 *    in the loop compute
    	 *    	segmentCounter, passOriginCounter
    	 *	maxlength, minlength, maxangle = -1.0,
    	 *      minangle, distribution[]
    	 * 2/ close the input (e.g. by using F1=null)
    	 * 3/ knowing number of segments to be entered
    	 *    define an array of segments (of that given size)
    	 * 4/ read the file again into this array
    	 * 5/ go through the pairs in the array and check
    	 *    whether they intersect. Be careful not to count
    	 *    intersections twice
    	 *
    	 *-----------------------------------------------------
    	 */
    	 
    	 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];
    	
    	F1 = null;
    	F1 = new MaInput("test.data");
    	while (!F1.atEOF()) {
    	
    		for ( int i=0; i<segmentCounter; i++ ) {
    			
    			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)
    				segments[i] = new LineSegment( Ax, Ay, Bx, By);
    		}
    		
    	}
    	
    	
    	for ( int n=0; n<segmentCounter; n++) {
    	
    		for ( int m=n+1; m<segmentCounter; m++) {
    		
    			if (segments[n].intersectSegment(segments[m])) intersectCounter++;
    		
    		}
    	}  	
    	
    	F1 = null;
        
        }
    
    
    
        /*****************************************************************
         * 
         * getNumberIntersectingSegments() - getter that allows the user 
         *                       to obtain the number of
         *                       intersecting pairs that are stored in a
         *                       private variable of this class
         *    
         * @param none
         *
         *****************************************************************
         */
    
        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 t = 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: " +passOriginCounter);
    	System.out.println("	Number of intersecting segments: " +t);
    	System.out.println("	Max length: " +MaF.dF(maxlength, 6, 2));
    	System.out.println("	Min length: " +MaF.dF(minlength, 6, 2));
    	System.out.println("	Max angle: " +MaF.dF(maxangle, 6, 2));
    	System.out.println("	Min angle: " +MaF.dF(minangle, 6, 2));
    	
    	System.out.println("	Angle (in deg) distribution:");
    	System.out.println("		[0.0, 18.0] : " +MaF.dF(distribution[0], 5, 3));
    	System.out.println("		[18.0, 36.0] : " +MaF.dF(distribution[1], 5, 3));
    	System.out.println("		[36.0, 54.0] : " +MaF.dF(distribution[2], 5, 3));
    	System.out.println("		[54.0, 72.0] : " +MaF.dF(distribution[3], 5, 3));
    	System.out.println("		[72.0, 90.0] : " +MaF.dF(distribution[4], 5, 3));
    	System.out.println("		[90.0, 108.0] : " +MaF.dF(distribution[5], 5, 3));
    	System.out.println("		[108.0, 126.0] : " +MaF.dF(distribution[6], 5, 3));
    	System.out.println("		[126.0, 144.0] : " +MaF.dF(distribution[7], 5, 3));
    	System.out.println("		[144.0, 162.0] : " +MaF.dF(distribution[8], 5, 3));
    	System.out.println("		[162.0, 180.0] : " +MaF.dF(distribution[9], 5, 3)); */
    
        }
    }
    I have now adjusted the code to the above following some advice from elsewhere, but I get the following error message on running the program:
    Unexpected end of file test.data line 5
    Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Double.valueOf(Unknown Source)
    at MaInput.readDouble(MaInput.java:995)
    at Project2.Results(Project2.java:139)
    at Project2.main(Project2.java:193)

    Help would be much appreciated, thanks.

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Problems seems to be in MaInput.java instead of the code you pasted here. You would want to debug your code keeping breakpoints at readDouble() method as well as look through the data.txt file.
    dont worry newbie, we got you covered.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 PM
  3. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 04:23 AM
  4. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM
  5. tomcat exception-error
    By Nick15 in forum Eclipse
    Replies: 2
    Last Post: 05-11-2007, 01:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •