| | |
null pointer exception?!?!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
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.
The null pointer exception occurs in line 63 when it goes to setPixel in the array. Here is my xpm class, which might help.
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
java Syntax (Toggle Plain Text)
// line class to store lines from .ps file class line { // for outcodes private int RIGHT = 2; private int TOP = 8; private int LEFT = 1; private int BOTTOM = 4; // two points signify each line private point point0; private point point1; public line(point _point1, point _point2) { point0 = _point1; point1 = _point2; } public void setPoint1(point _new) { point0 = _new; } public void setPoint2 (point _another) { point1 = _another; } public point getPoint1() { return point0; } public point getPoint2() { return point1; } public float getSlope() { int rise = point0.getY() - point1.getY(); int run = point0.getX() - point1.getX(); float slope = rise/run; return slope; } // only for drawing // AFTER clipping public void DDA(xpm _file) { int x1 = this.getPoint1().getX(); int y1 = this.getPoint1().getY(); int x2 = this.getPoint2().getX(); int y2 = this.getPoint2().getY(); double m = (double)(y2-y1)/(x2-x1); double y = (double)y1; int iy; for (int x = x1 ; x <= x2 ; x++) { iy = (int)Math.round(y); setPixel(x, iy, _file); y += m; } } // actually setting the correct pixel in .XPM private void setPixel(int x, int iy, xpm _file) { _file.pixels[x][iy].setValue("b"); } // getting the codes for the inputted lines // find out where to clip them public int ComputeOutCode (int x, int y, int xmin, int ymin, int xmax, int ymax) { int code = 0; if (y > ymax) code |= TOP; else if (y < ymin) code |= BOTTOM; if (x > xmax) code |= RIGHT; else if (x < xmin) code |= LEFT; return code; } // clip the lines right before drawing them public void cohenSutherland( int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax, int ymax) { //Outcodes for P0, P1, and whatever point lies outside the clip rectangle int outcode0, outcode1, outcodeOut, hhh = 0; boolean accept = false, done = false; //compute outcodes outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax); outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax); //System.out.println( outcode0 + " " + outcode1 ); do{ if ((outcode0 | outcode1) == 0 ) { accept = true; done = true; } else if ( (outcode0 & outcode1) > 0 ) { done = true; } else { //failed both tests, so calculate the line segment to clip //from an outside point to an intersection with clip edge int x = 0, y = 0; //At least one endpoint is outside the clip rectangle; pick it. outcodeOut = outcode0 != 0 ? outcode0: outcode1; //Now find the intersection point; //use formulas y = y0 + slope * (x - x0), x = x0 + (1/slope)* (y - y0) if ( (outcodeOut & TOP) > 0 ) { x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1); y = ymax; } else if ((outcodeOut & BOTTOM) > 0 ) { x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1); y = ymin; } else if ((outcodeOut & RIGHT)> 0) { y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1); x = xmax; } else if ((outcodeOut & LEFT) > 0) { y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1); x = xmin; } //Now we move outside point to intersection point to clip //and get ready for next pass. if (outcodeOut == outcode0) { x1 = x; y1 = y; outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax); } else { x2 = x; y2 = y; outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax); } } hhh ++; } while (done != true && hhh < 5000); if(accept) { set( x1, y1, x2, y2); } } private void set(int x1, int y1, int x2, int y2) { point newPoint1 = new point(x1,y1); point newPoint2 = new point(x2,y2); this.setPoint1(newPoint1); this.setPoint2(newPoint2); } }
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)
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; // xpm class // aka frame buffer public class xpm { public pixel[][] pixels; private String header = "/* XPM */"; private String array = "static char *sco100[] = { "; private String header2 = " /* width height num_colors chars_per_pixel */ "; private String dimensions = " \"500 500 2 1\", "; private String header3 = " /* colors */ "; private String white = " \"- c #ffffff\", "; private String black = " \"b c #000000\", "; private String header4 = " /* pixels */ "; private String pixelsString = ""; private String end = "};"; public xpm() { pixels = new pixel[500][500]; } public void makePixelsString() { String thisLine = ""; for (int i = 0; i < pixels.length; i++) { thisLine += "\""; for (int j = 0; j < pixels[i].length; j++) { thisLine += pixels[i][j].getValue(); } if (i == pixels.length - 1) { thisLine += "\"\n"; } else { thisLine += "\",\n"; } } pixelsString = thisLine; } public pixel getArraySpot(int x, int y) { return pixels[x][y]; } public void makeXPM() throws IOException { String text = header + array + header2 + dimensions + header3 + white + black + header4 + pixelsString + end; // add pixels.. File file = new File("out.xpm"); BufferedWriter output = new BufferedWriter(new FileWriter(file)); output.write(text); output.close(); } }
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.
1
#2 Oct 8th, 2009
Usually a NullPointerException means that you are trying to use something that is null. So if you get the exception at line 63:
it would mean that: "_file.pixels[x][iy]" is null. So how do you populate that array?
Java Syntax (Toggle Plain Text)
_file.pixels[x][iy].setValue("b");
it would mean that: "_file.pixels[x][iy]" is null. So how do you populate that array?
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Getting a null pointer exception when saving image to file. (Java)
- Null Pointer Exception problem (Java)
- Null Pointer Exception....... (Java)
- Null pointer exception while getting other applet in the same page (Java)
- null pointer exception --- urgent (Java)
- Java Null Pointer Exception (Java)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: Problem in counting number of ArrayList object in ArrayList
- Next Thread: Hello World
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






