Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: NullPointerException, but can't figure out why

  1. #1
    Junior Member Aeleck's Avatar
    Join Date
    Oct 2014
    Location
    Glendale, Arizona
    Posts
    6
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy NullPointerException, but can't figure out why

    I'm working on developing my own engine, and at the moment, I have a line segment class that, given a starting x and y, and a ending x and y, it generates all the points based on a 0.02f unit size. What I mean is that it works like pixels. I only want a solid unit square to light up if the line approaches it in a way similar to Bresenham's Algorithm. It stores these points in a array using my point class (which consists of a x(float), y(float), z(float), and velocity(Vector) and returns this array. When I iterate through the array some of the points come back as null and I've tried checking for when one of the points it just created is null, and its able to give me valid x and y for the point (the reason why its only x and y, is because for now, im ignoring the z axis and am only trying to simulate a pixel screen on a 2d surface, so the z axis is ignored), yet the point still says its null. And its several points, and it changes each time (the line is randomly generated). LineSegment class is as follows:

    import java.math.BigDecimal;
     
    import javax.media.j3d.Appearance;
    import javax.media.j3d.Material;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.vecmath.Color3f;
    import javax.vecmath.Vector3f;
     
    import com.sun.j3d.utils.geometry.Box;
     
    public class LineSegment {
     
    	float x1, x2;
    	float y1, y2;
    	Integer count = 0;
     
    	public LineSegment(float x1, float x2, float y1, float y2) {
    		this.x1 = x1;
    		this.x2 = x2;
    		this.y1 = y1;
    		this.y2 = y2;
    	}
     
    	public static float round(float d, int decimalPlace) {
    		BigDecimal bd = new BigDecimal(Float.toString(d));
    		bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    		return bd.floatValue();
    	}
     
    	public void drawSegmentPoint(Point point) {
    		Color3f color = new Color3f(1.0f, 0.1f, 0.1f);
    		if (count == 0) {
    			color = new Color3f(1.0f, 0.1f, 0.1f);
    			count++;
    		} else if (count == 1) {
    			color = new Color3f(0.1f, 1.0f, 0.1f);
    			count++;
    		} else if (count == 2) {
    			color = new Color3f(0.1f, 0.1f, 1.0f);
    			count = 0;
    		}
    		Appearance app = new Appearance();
    		Material mat = new Material();
    		mat.setDiffuseColor(color);
    		mat.setSpecularColor(color);
    		app.setMaterial(mat);
    		Box box = new Box(0.01f, 0.01f, 0.01f, app);
    		TransformGroup tg = new TransformGroup();
    		Transform3D transform = new Transform3D();
    		Vector3f vector = new Vector3f(point.x, point.y, point.z);
    		transform.setTranslation(vector);
    		tg.setTransform(transform);
    		tg.addChild(box);
    		Main.segmentGroup.addChild(tg);
    	}
     
    	public Point[] returnPointsInSegment() {
     
    		float x1 = this.x1;
    		float y1 = this.y1;
    		float x2 = this.x2;
    		float y2 = this.y2;
    		float z = 0.0f;
     
    		Point[] pointArray;
     
    		float dx = x2 - x1;
    		float dy = y2 - y1;
     
    		if ((dy / dx) > 1 || (dy / dx) < -1) {
    			if (y2 < y1) {
    				float tempx1 = x2;
    				float tempx2 = x1;
    				x1 = tempx1;
    				x2 = tempx2;
    				float tempy1 = y2;
    				float tempy2 = y1;
    				y1 = tempy1;
    				y2 = tempy2;
    			}
    		} else {
    			if (x2 < x1) {
    				float tempx1 = x2;
    				float tempx2 = x1;
    				x1 = tempx1;
    				x2 = tempx2;
    				float tempy1 = y2;
    				float tempy2 = y1;
    				y1 = tempy1;
    				y2 = tempy2;
    			}
    		}
     
    		if (x2 == x1 || (dy / dx) > 1 || (dy / dx) < -1) {
    			float ry1 = y1;
    			float ry2 = y2;
    			Integer je = (int) Math.floor(round(ry1 * 100.0f, 0));
    			if ((je % 2) != 0) {
    				ry1 -= 0.01f;
    			}
    			ry1 = round(ry1, 2);
    			Integer je2 = (int) Math.floor(round(ry2 * 100.0f, 0));
    			if ((je2 % 2) != 0) {
    				ry2 += 0.01f;
    			}
    			ry2 = round(ry2, 2);
    			pointArray = new Point[(int) (Math
    					.floor(Math.abs(ry2 - ry1) / 0.02f)) + 1];
    		} else {
    			float rx1 = x1;
    			float rx2 = x2;
    			Integer je = (int) Math.floor(round(rx1 * 100.0f, 0));
    			if ((je % 2) != 0) {
    				rx1 -= 0.01f;
    			}
    			rx1 = round(rx1, 2);
    			Integer je2 = (int) Math.floor(round(rx2 * 100.0f, 0));
    			if ((je2 % 2) != 0) {
    				rx2 += 0.01f;
    			}
    			rx2 = round(rx2, 2);
    			pointArray = new Point[(int) (Math
    					.floor(Math.abs(rx2 - rx1) / 0.02f)) + 1];
    		}
     
    		if (dx == 0) {
    			for (float y = 0; y1 + y <= y2; y += 0.02) {
    				y = round(y, 2);
    				pointArray[(int) Math.floor(y / 0.02f)] = new Point(x1, y1 + y,
    						z, new Vector(0.0f, 0.0f, 0.0f));
    			}
    			return pointArray;
    		}
     
    		float b = y1 - ((dy / dx) * x1);
     
    		if (dy / dx > 1 || dy / dx < -1) {
    			Integer je = (int) Math.floor(round(y1 * 100.0f, 0));
    			if ((je % 2) != 0) {
    				y1 -= 0.01f;
    			}
    			y1 = round(y1, 2);
    			for (float y = 0; y1 + y <= y2; y += 0.02) {
    				round(y, 2);
    				float x = (((y + y1) - b) / (dy / dx));
    				x = round(x, 2);
    				Integer te = (int) Math.floor(round(x * 100.0f, 0));
    				if ((te % 2) != 0) {
    					x -= 0.01f;
    				}
    				Integer index = (int) Math.floor(y / 0.02f);
    				pointArray[index] = new Point(round(x, 2), round(y + y1, 2), z,
    						new Vector(0.0f, 0.0f, 0.0f));
    				for (Point point : pointArray) {
    					if (point == null) {
    						System.out.println(index + " : Index : "
    								+ round(x1 + x, 2) + " : X : " + round(y, 2)
    								+ " : Y");
    					}
    				}
    			}
    		} else {
    			Integer je = (int) Math.floor(round(x1 * 100.0f, 0));
    			if ((je % 2) != 0) {
    				x1 -= 0.01f;
    			}
    			x1 = round(x1, 2);
    			for (float x = 0; x1 + x <= x2; x += 0.02) {
    				round(x, 2);
    				float y = (((dy / dx) * (x1 + x)) + b);
    				y = round(y, 2);
    				Integer te = (int) Math.floor(round(y * 100.0f, 0));
    				if ((te % 2) != 0) {
    					y -= 0.01f;
    				}
    				Integer index = (int) Math.floor(x / 0.02f);
    				pointArray[index] = new Point(round(x1 + x, 2), round(y, 2), z,
    						new Vector(0.0f, 0.0f, 0.0f));
    				for (Point point : pointArray) {
    					if (point == null) {
    						System.out.println(index + " : Index : "
    								+ round(x1 + x, 2) + " : X : " + round(y, 2)
    								+ " : Y");
    					}
    				}
     
    			}
    		}
    		return pointArray;
    	}
    }

    Oh and if I code horribly, I apologize and would deeply appreciate tips and pointers. Thank you.
    Last edited by Aeleck; October 6th, 2014 at 11:07 AM. Reason: Afterthought


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: NullPointerException, but can't figure out why

    A lot of code posted (correctly, thank you), and I'm not sure what we're looking for or what we're supposed to do to see the problem. Can you post an error message, sample run, code that we can run to demonstrate the problem with instructions, something to show us what the problem looks like?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Aeleck (October 6th, 2014)

  4. #3
    Junior Member Aeleck's Avatar
    Join Date
    Oct 2014
    Location
    Glendale, Arizona
    Posts
    6
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException, but can't figure out why

    Yes, sorry about that. So there's two areas where it's cycling through my x or y values to determine where the partner x or y should be and they look like this:

    for (float y = 0; y1 + y <= y2; y += 0.02) {
    				round(y, 2);
    				float x = (((y + y1) - b) / (dy / dx));
    				x = round(x, 2);
    				Integer te = (int) Math.floor(round(x * 100.0f, 0));
    				if ((te % 2) != 0) {
    					x -= 0.01f;
    				}
    				Integer index = (int) Math.floor(y / 0.02f);
    				pointArray[index] = new Point(round(x, 2), round(y + y1, 2), z,
    						new Vector(0.0f, 0.0f, 0.0f));
    				for (Point point : pointArray) {
    					if (point == null) {
    						System.out.println(index + " : Index : "
    								+ round(x1 + x, 2) + " : X : " + round(y, 2)
    								+ " : Y");
    					}
    				}
    			}

    And there's one for the x values as well (it uses y if the slope is greater than 1 or less than -1 and x if its less than or equal to 1 or greater than or equal to -1), and in this section, it assigns this point to my Point array that is defined above. In a different class (a Render class used for displaying the LineSegment), I had attempted access the point and it return null (will post error below). So I set up the little for statements directly beneath where it assigns the points to the array to see if it actually is the points that are null and some are returning null; however, I do not see how they could be if I'm able to view the x, y, and z of the point, along with the index of the array it's at.

    for (Point point : pointArray) {
    					if (point == null) {
    						System.out.println(index + " : Index : "
    								+ round(x1 + x, 2) + " : X : " + round(y, 2)
    								+ " : Y");
    					}
    				}

    And it finds that every point on every line except the last two, return null yet console will display "23 : Index : 0.6 : X : 0.22 : Y" and later I checked to see if my z is causing an issue, it's not. In my Render class, when it cycle's through all the assigned points, I have it check for null and only display the ones that are not null, and when the lines are drawn, they usually have 1 or two holes in the line, which should be my null points. However once again, if apparently so many points are null, why can I display their x and y, and then when I go to graph, only a couple are missing yet it returned that almost all of them are null.

    Picture of SimpleUniverse, plotting a 2d line on the xy axis with checking for null points, and only plotting those points that are not null:

    Screenshot (9).jpg

    The following image is when it looks through the array right after setting every point, and only displaying the points that are null. It's still able to view its x and y

    Screenshot (10).jpg

    And this is the error message I get if I don't check for the points being null (because they shouldn't be because every index of the array has been filled with proper values, hence why I print out all the values).

    Screenshot (11).jpg
    Last edited by Aeleck; October 6th, 2014 at 11:08 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,199
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: NullPointerException, but can't figure out why

    Can you copy the full text of the error message and paste it here. No image. They are too small to read and text can't be copied from them to include in a response.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member Aeleck's Avatar
    Join Date
    Oct 2014
    Location
    Glendale, Arizona
    Posts
    6
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException, but can't figure out why

    Sorry

    Below is the Renderer class which is used to display objects in the SimpleUniverse.

    public class Renderer {
     
    	Integer numbofobj = 0;
    	Integer totalObjects = 0;
    	Boolean pointGroupEnabled = true;
    	Boolean segmentGroupEnabled = true;
     
    	public Renderer() {
     
    	}
     
    	Object[] objectsInWorld = new Object[2];
     
    	// NOTE: [0] = Points
    	// 1 "unit" = 0.02f
     
    	/**
    	 * Adds points to proper BranchGroup. camera is the default generated
    	 * Camera, object is an intance of the object in which will be redrawn,
    	 * exclusive is a boolean which indicates whether the object should be the
    	 * only object to be drawn
    	 * 
    	 * NOTE: If the object is null, and exclusive is set to true, then the scene
    	 * will only refresh what is currently being displayed already, instead of
    	 * every possible object
    	 * 
    	 * @param camera
    	 * @param object
    	 * @param exclusive
    	 */
    	public void renderSceneC(Camera camera, Object object, Boolean exclusive) {
    		// passing in any instance of the object will only redraw that specific
    		// object
    		// passing in NULL will be default to redraw every object
    		if (exclusive) {
    			Main.resetAllBranchGroups();
    		}
    		numbofobj = 0;
    		totalObjects = 0;
    		for (int x = 0; x < objectsInWorld.length; x++) {
    			if (objectsInWorld[x] instanceof Point[]) {
    				if (object instanceof Point || object == null) {
    					if (object == null && exclusive == true) {
    						if (pointGroupEnabled) {
    							Point[] points = (Point[]) objectsInWorld[x];
    							for (Point point : points) {
    								Point camloc = new Point(camera.location.x,
    										camera.location.y, camera.location.z,
    										new Vector(0.0f, 0.0f, 0.0f));
    								Vector dis = point
    										.subtractPointFromPoint(camloc);
    								float distance = dis.findDistance();
    								if (distance <= camera.distance) {
    									point.drawPoint(0.02f);
    									numbofobj++;
    								}
    							}
    						}
    					} else {
    						pointGroupEnabled = true;
    						Point[] points = (Point[]) objectsInWorld[x];
    						for (Point point : points) {
    							Point camloc = new Point(camera.location.x,
    									camera.location.y, camera.location.z,
    									new Vector(0.0f, 0.0f, 0.0f));
    							Vector dis = point.subtractPointFromPoint(camloc);
    							float distance = dis.findDistance();
    							if (distance <= camera.distance) {
    								point.drawPoint(0.02f);
    								numbofobj++;
    							}
    						}
    					}
    				} else {
    					pointGroupEnabled = false;
    				}
    			} else if (objectsInWorld[x] instanceof LineSegment[]) {
    				if (object instanceof LineSegment || object == null) {
    					if (object == null && exclusive == true) {
    						if (segmentGroupEnabled) {
    							LineSegment[] ls = (LineSegment[]) objectsInWorld[x];
    							for (LineSegment line : ls) {
    								Point[] points = line.returnPointsInSegment();
    								for (Point p : points) {
    									Point camloc = new Point(camera.location.x,
    											camera.location.y,
    											camera.location.z, new Vector(0.0f,
    													0.0f, 0.0f));
    									Vector dis = p
    											.subtractPointFromPoint(camloc);
    									float distance = dis.findDistance();
    									if (distance <= camera.distance) {
    										line.drawSegmentPoint(p);
    										numbofobj++;
    									}
     
    								}
    							}
    						}
    					} else {
    						segmentGroupEnabled = true;
    						LineSegment[] ls = (LineSegment[]) objectsInWorld[x];
    						for (LineSegment line : ls) {
    							Point[] points = line.returnPointsInSegment();
    							for (Point p : points) {
    								Point camloc = new Point(camera.location.x,
    										camera.location.y, camera.location.z,
    										new Vector(0.0f, 0.0f, 0.0f));
    								Vector dis = p.subtractPointFromPoint(camloc);
    								float distance = dis.findDistance();
    								if (distance <= camera.distance) {
    									line.drawSegmentPoint(p);
    									numbofobj++;
    								}
    							}
    						}
    					}
    				} else {
    					segmentGroupEnabled = false;
    				}
    			}
    		}
    		for (int x = 0; x < objectsInWorld.length; x++) {
    			if (objectsInWorld[x] instanceof Point[]) {
    				Point[] points = (Point[]) objectsInWorld[x];
    				for (Point point : points) {
    					Point camloc = new Point(camera.location.x,
    							camera.location.y, camera.location.z, new Vector(
    									0.0f, 0.0f, 0.0f));
    					Vector dis = point.subtractPointFromPoint(camloc);
    					float distance = dis.findDistance();
    					if (distance <= camera.distance) {
    						totalObjects++;
    					}
    				}
    			}
    			if (objectsInWorld[x] instanceof LineSegment[]) {
    				LineSegment[] ls = (LineSegment[]) objectsInWorld[x];
    				for (LineSegment line : ls) {
    					Point[] points = line.returnPointsInSegment();
    					for (Point point : points) {
    						Point camloc = new Point(camera.location.x,
    								camera.location.y, camera.location.z,
    								new Vector(0.0f, 0.0f, 0.0f));
    						Vector dis = point.subtractPointFromPoint(camloc);
    						float distance = dis.findDistance();
    						if (distance <= camera.distance) {
    							totalObjects++;
    						}
    					}
    				}
    			}
    		}
     
    		Main.northlabel.setText("Welcome | Key Pressed: " + Main.keypressed
    				+ " | Number of Objects Refreshed : " + numbofobj + " (Total: "
    				+ totalObjects + ")");
    	}
    }

    Where p is a point in the array being define. Any reference to p of any kind, returns the following error.

    Exception in thread "main" java.lang.NullPointerException
    at com.threed.engine.Renderer.renderSceneC(Renderer.j ava:111)
    at com.threed.engine.Main.createSceneGraph(Main.java: 344)
    at com.threed.engine.Main.redrawScene(Main.java:167)
    at com.threed.engine.Main.<init>(Main.java:416)
    at com.threed.engine.Main.main(Main.java:423)

    In this case line 111 is:

    Vector dis = p.subtractPointFromPoint(camloc);
    Last edited by Aeleck; October 6th, 2014 at 11:08 AM. Reason: Added entire Renderer class

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,199
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: NullPointerException, but can't figure out why

    Does p have the null value when that statement is executed?
    Why doesn't it have a non-null value?

    Does the points array have null values? Use the Arrays class's method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    to see what it contains.

    NOTE:
    Single letter variable names make it hard to do a Find for the variable's usage because common letters are found in many words. Unique names make it easier to Find.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Aeleck (October 6th, 2014)

  9. #7
    Junior Member Aeleck's Avatar
    Join Date
    Oct 2014
    Location
    Glendale, Arizona
    Posts
    6
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException, but can't figure out why

    That is true.. I guess I was getting lazy. Thanks! And yes, anytime I run through all the points in the array, some of them equal null. And I am trying to figure out why it is null, when it puts in valid arguments.

    Upon using
    System.out.println("an ID " + java.util.Arrays.toString(pointArray));

    It gave out:

    an ID [com.threed.engine.Point@168f27fc, com.threed.engine.Point@108a1cf6, com.threed.engine.Point@5b7362f5, com.threed.engine.Point@7465f1ba, com.threed.engine.Point@50e12609, com.threed.engine.Point@6d88425a, com.threed.engine.Point@cbc5ae4, com.threed.engine.Point@21f33544, com.threed.engine.Point@662e14c5, com.threed.engine.Point@5b55c5eb, com.threed.engine.Point@5c34f625, com.threed.engine.Point@190c266e, null, com.threed.engine.Point@6caf0c9a, com.threed.engine.Point@58648a34, com.threed.engine.Point@30ad8942, com.threed.engine.Point@510e6505, com.threed.engine.Point@80a54d6, com.threed.engine.Point@5e47b1b9, com.threed.engine.Point@34e5190a, com.threed.engine.Point@2ccefaa7, com.threed.engine.Point@48aa00ec, com.threed.engine.Point@1122ab39, com.threed.engine.Point@7b1733f8, com.threed.engine.Point@30551dea, com.threed.engine.Point@241f0670, com.threed.engine.Point@70dfb596]

    So in that one, there was one null.

    SOLVED:

    I found that there was two problems, first, I wasn't rounding the increment value (since floats aren't always accurate).

    round(x, 2) // old code
    x = round(x, 2) // this is what it should have been since round returns the float

    Also, in the making of the for statement:

    for (float y = 0; y1 + y <= y2; y += 0.02) { // OLD CODE
    				round(y, 2);
    				float x = (((y + y1) - b) / (dy / dx));
    				x = round(x, 2);
    				Integer te = (int) Math.floor(round(x * 100.0f, 0));
    				if ((te % 2) != 0) {
    					x -= 0.01f;
    				}
    				Integer index = (int) Math.floor(y / 0.02f);
    				pointArray[index] = new Point(round(x, 2), round(y + y1, 2), z,
    						new Vector(0.0f, 0.0f, 0.0f));
    				for (Point point : pointArray) {
    					if (point == null) {
    						System.out.println(index + " : Index : "
    								+ round(x1 + x, 2) + " : X : " + round(y, 2)
    								+ " : Y");
    					}
    				}
    			}

    I changed it from y1 + y <= y2 to round(y/0.02f, 0) < pointArray.length and now it works. No more points are null.

    Thanks for your help! I love the method to right out arrays. I didn't realize that could be used to see null entries.
    Last edited by Aeleck; October 6th, 2014 at 01:09 PM.

Similar Threads

  1. Cant Figure it out. Can someone help me?
    By EricIsaiah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 28th, 2014, 02:17 AM
  2. Cant figure this out
    By lisp1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2013, 06:46 AM
  3. I can't figure this out! Please help!
    By angyvill85 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2012, 03:36 AM
  4. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 01:26 PM
  5. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM

Tags for this Thread