944,030 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 456
  • Java RSS
Oct 7th, 2009
0

null pointer exception?!?!

Expand Post »
So I have a lot of code so I guess I'll just post the couple classes that seem to be giving me problems and where the problem is occuring.

java Syntax (Toggle Plain Text)
  1. // line class to store lines from .ps file
  2. class line {
  3.  
  4. // for outcodes
  5. private int RIGHT = 2;
  6. private int TOP = 8;
  7. private int LEFT = 1;
  8. private int BOTTOM = 4;
  9.  
  10.  
  11. // two points signify each line
  12. private point point0;
  13. private point point1;
  14.  
  15. public line(point _point1, point _point2) {
  16. point0 = _point1;
  17. point1 = _point2;
  18. }
  19. public void setPoint1(point _new) {
  20. point0 = _new;
  21. }
  22.  
  23. public void setPoint2 (point _another) {
  24. point1 = _another;
  25. }
  26.  
  27. public point getPoint1() {
  28. return point0;
  29. }
  30.  
  31. public point getPoint2() {
  32. return point1;
  33. }
  34.  
  35. public float getSlope() {
  36. int rise = point0.getY() - point1.getY();
  37. int run = point0.getX() - point1.getX();
  38.  
  39. float slope = rise/run;
  40. return slope;
  41. }
  42.  
  43. // only for drawing
  44. // AFTER clipping
  45. public void DDA(xpm _file) {
  46. int x1 = this.getPoint1().getX();
  47. int y1 = this.getPoint1().getY();
  48. int x2 = this.getPoint2().getX();
  49. int y2 = this.getPoint2().getY();
  50.  
  51. double m = (double)(y2-y1)/(x2-x1);
  52. double y = (double)y1;
  53. int iy;
  54. for (int x = x1 ; x <= x2 ; x++) {
  55. iy = (int)Math.round(y);
  56. setPixel(x, iy, _file);
  57. y += m;
  58. }
  59. }
  60.  
  61. // actually setting the correct pixel in .XPM
  62. private void setPixel(int x, int iy, xpm _file) {
  63. _file.pixels[x][iy].setValue("b");
  64. }
  65.  
  66. // getting the codes for the inputted lines
  67. // find out where to clip them
  68. public int ComputeOutCode (int x, int y, int xmin, int ymin, int xmax, int ymax)
  69. {
  70. int code = 0;
  71. if (y > ymax)
  72. code |= TOP;
  73. else if (y < ymin)
  74. code |= BOTTOM;
  75. if (x > xmax)
  76. code |= RIGHT;
  77. else if (x < xmin)
  78. code |= LEFT;
  79. return code;
  80. }
  81.  
  82. // clip the lines right before drawing them
  83. public void cohenSutherland( int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax, int ymax)
  84. {
  85. //Outcodes for P0, P1, and whatever point lies outside the clip rectangle
  86. int outcode0, outcode1, outcodeOut, hhh = 0;
  87. boolean accept = false, done = false;
  88.  
  89. //compute outcodes
  90. outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax);
  91. outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax);
  92.  
  93. //System.out.println( outcode0 + " " + outcode1 );
  94.  
  95. do{
  96. if ((outcode0 | outcode1) == 0 )
  97. {
  98. accept = true;
  99. done = true;
  100. }
  101. else if ( (outcode0 & outcode1) > 0 )
  102. {
  103. done = true;
  104. }
  105.  
  106. else
  107. {
  108. //failed both tests, so calculate the line segment to clip
  109. //from an outside point to an intersection with clip edge
  110. int x = 0, y = 0;
  111. //At least one endpoint is outside the clip rectangle; pick it.
  112. outcodeOut = outcode0 != 0 ? outcode0: outcode1;
  113. //Now find the intersection point;
  114. //use formulas y = y0 + slope * (x - x0), x = x0 + (1/slope)* (y - y0)
  115. if ( (outcodeOut & TOP) > 0 )
  116. {
  117. x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
  118. y = ymax;
  119. }
  120. else if ((outcodeOut & BOTTOM) > 0 )
  121. {
  122. x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
  123. y = ymin;
  124. }
  125. else if ((outcodeOut & RIGHT)> 0)
  126. {
  127. y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
  128. x = xmax;
  129. }
  130. else if ((outcodeOut & LEFT) > 0)
  131. {
  132. y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
  133. x = xmin;
  134. }
  135. //Now we move outside point to intersection point to clip
  136. //and get ready for next pass.
  137. if (outcodeOut == outcode0)
  138. {
  139. x1 = x;
  140. y1 = y;
  141. outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax);
  142. }
  143. else
  144. {
  145. x2 = x;
  146. y2 = y;
  147. outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax);
  148. }
  149. }
  150. hhh ++;
  151. }
  152. while (done != true && hhh < 5000);
  153.  
  154.  
  155. if(accept)
  156. {
  157. set( x1, y1, x2, y2);
  158. }
  159.  
  160. }
  161.  
  162.  
  163. private void set(int x1, int y1, int x2, int y2) {
  164. point newPoint1 = new point(x1,y1);
  165. point newPoint2 = new point(x2,y2);
  166. this.setPoint1(newPoint1);
  167. this.setPoint2(newPoint2);
  168.  
  169. }
  170.  
  171. }

The null pointer exception occurs in line 63 when it goes to setPixel in the array. Here is my xpm class, which might help.

java Syntax (Toggle Plain Text)
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6. // xpm class
  7. // aka frame buffer
  8.  
  9. public class xpm {
  10.  
  11. public pixel[][] pixels;
  12.  
  13. private String header = "/* XPM */";
  14.  
  15. private String array = "static char *sco100[] = { ";
  16.  
  17. private String header2 = " /* width height num_colors chars_per_pixel */ ";
  18.  
  19. private String dimensions = " \"500 500 2 1\", ";
  20.  
  21. private String header3 = " /* colors */ ";
  22.  
  23. private String white = " \"- c #ffffff\", ";
  24.  
  25. private String black = " \"b c #000000\", ";
  26.  
  27. private String header4 = " /* pixels */ ";
  28.  
  29. private String pixelsString = "";
  30.  
  31. private String end = "};";
  32.  
  33. public xpm() {
  34.  
  35. pixels = new pixel[500][500];
  36. }
  37.  
  38. public void makePixelsString() {
  39.  
  40. String thisLine = "";
  41. for (int i = 0; i < pixels.length; i++) {
  42. thisLine += "\"";
  43. for (int j = 0; j < pixels[i].length; j++) {
  44. thisLine += pixels[i][j].getValue();
  45. }
  46. if (i == pixels.length - 1) {
  47. thisLine += "\"\n";
  48. }
  49. else {
  50. thisLine += "\",\n";
  51. }
  52. }
  53.  
  54. pixelsString = thisLine;
  55. }
  56.  
  57. public pixel getArraySpot(int x, int y) {
  58. return pixels[x][y];
  59. }
  60.  
  61. public void makeXPM() throws IOException {
  62. String text = header + array + header2 + dimensions + header3 + white + black + header4
  63. + pixelsString + end;
  64. // add pixels..
  65.  
  66.  
  67. File file = new File("out.xpm");
  68.  
  69. BufferedWriter output = new BufferedWriter(new FileWriter(file));
  70. output.write(text);
  71. output.close();
  72. }
  73. }

Also if you have any other pointers or need to see more code I can provide it for you.

This thing is due by tonight at midnight, it's 8:17pm here right now
Last edited by gotm; Oct 7th, 2009 at 9:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Oct 8th, 2009
1
Re: null pointer exception?!?!
Usually a NullPointerException means that you are trying to use something that is null. So if you get the exception at line 63:

Java Syntax (Toggle Plain Text)
  1. _file.pixels[x][iy].setValue("b");

it would mean that: "_file.pixels[x][iy]" is null. So how do you populate that array?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem in counting number of ArrayList object in ArrayList
Next Thread in Java Forum Timeline: Hello World





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC