Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2008, 05:28 PM
Member
 
Join Date: Mar 2008
Posts: 2
brownie_jedi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-15-2008, 03:01 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
please include the stack trace .. i hate going through the code ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-15-2008, 03:16 AM
Member
 
Join Date: Mar 2008
Posts: 2
brownie_jedi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-15-2008, 07:27 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Error need help fixing skinnybones New To Java 2 12-03-2007 08:14 PM
statement null pointer exception bbq Database 1 07-05-2007 05:23 AM
JSF error+exception Peter SWT / JFace 1 07-04-2007 07:29 AM
tomcat exception-error Nick15 Eclipse 2 05-11-2007 02:32 AM
Null pointer Exception peiceonly New To Java 2 04-06-2007 03:41 PM


All times are GMT +3. The time now is 11:30 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org