assault_leader
New member
משום מה הקוד ג´אווה הדבילי לא עובד! ../images/Emo7.gif
הכל מתקמפל כמו שצריך אבל בכל פעם נזרקת חריגת NullPointerException ואין לי מושג למה, חוץ מזה תפסתי את השגיאות בכל הפעמים אחרי ביצוע של Point.toString וRectangle.details וזה ממש מעצבן.....הקבצים מצורפים.... public class Point extends Object { private double x; private double y; public Point() { } public void setX(double x){ if (x > 0) this.x = x; } public void setY(double y){ if (y > 0) this.y = y; } public Point(double x,double y) { try { System.out.println("building a point right now.."); if (y > 0) this.y = y; if (x > 0) this.x = x; } catch (Exception e) { System.out.println("Caught it at Point(dbl,dbl).."); } } public Point(Point p) { x = p.x; y = p.y; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "\n X Value \n" + this.x + "\n Y Value \n" + this.y + " \n "; } } ========================================= public class Rectangle extends Object { private Point upperLeft; private Point bottomRight; public Rectangle(){ } public Rectangle(Point upperLeft,Point bottomRight){ upperLeft = new Point(upperLeft); bottomRight = new Point(bottomRight); System.out.println("Been there, done that.."); } public Rectangle(Rectangle rectangle){ this.upperLeft = new Point(rectangle.upperLeft); this.bottomRight = new Point(rectangle.bottomRight); } public void details(){ try { Point upperRight = new Point(bottomRight.getX(),upperLeft.getY()); System.out.println(upperRight.toString()); } catch(Exception e) { System.out.println("Caught it at Rect.details1.."); } try { Point bottomLeft = new Point(upperLeft.getX(),bottomRight.getY()); System.out.println(bottomLeft.toString()); } catch(Exception e) { System.out.println("Caught it at Rect.details2.."); } System.out.println("The 4 points of the Rectangle"); System.out.println(upperLeft.toString()); System.out.println(bottomRight.toString()); } public void addRect(Rectangle rect2){ try { upperLeft.setY(Math.max(upperLeft.getY(),rect2.upperLeft.getY())); upperLeft.setX(Math.min(upperLeft.getX(),rect2.upperLeft.getX())); bottomRight.setX(Math.max(bottomRight.getX(),rect2.bottomRight.getX())); bottomRight.setY(Math.min(bottomRight.getY(),rect2.bottomRight.getY())); } catch (NullPointerException e) { System.out.println("Caught it at addRect..."); } } } ================= public class TryRect extends Object { public static void main(String[] args) { System.out.println("Gutten Tag.."); Point p1 = new Point(20.5,34.5); Point p2 = new Point(21.5,56.5); Point p3 = new Point(26.5,86.5); Point p4 = new Point(28.5,98.5); Rectangle rect1 = new Rectangle(p1,p2); Rectangle rect2 = new Rectangle(p3,p4); rect1.details(); rect2.details(); rect1.addRect(rect2); rect1.details(); } }
הכל מתקמפל כמו שצריך אבל בכל פעם נזרקת חריגת NullPointerException ואין לי מושג למה, חוץ מזה תפסתי את השגיאות בכל הפעמים אחרי ביצוע של Point.toString וRectangle.details וזה ממש מעצבן.....הקבצים מצורפים.... public class Point extends Object { private double x; private double y; public Point() { } public void setX(double x){ if (x > 0) this.x = x; } public void setY(double y){ if (y > 0) this.y = y; } public Point(double x,double y) { try { System.out.println("building a point right now.."); if (y > 0) this.y = y; if (x > 0) this.x = x; } catch (Exception e) { System.out.println("Caught it at Point(dbl,dbl).."); } } public Point(Point p) { x = p.x; y = p.y; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "\n X Value \n" + this.x + "\n Y Value \n" + this.y + " \n "; } } ========================================= public class Rectangle extends Object { private Point upperLeft; private Point bottomRight; public Rectangle(){ } public Rectangle(Point upperLeft,Point bottomRight){ upperLeft = new Point(upperLeft); bottomRight = new Point(bottomRight); System.out.println("Been there, done that.."); } public Rectangle(Rectangle rectangle){ this.upperLeft = new Point(rectangle.upperLeft); this.bottomRight = new Point(rectangle.bottomRight); } public void details(){ try { Point upperRight = new Point(bottomRight.getX(),upperLeft.getY()); System.out.println(upperRight.toString()); } catch(Exception e) { System.out.println("Caught it at Rect.details1.."); } try { Point bottomLeft = new Point(upperLeft.getX(),bottomRight.getY()); System.out.println(bottomLeft.toString()); } catch(Exception e) { System.out.println("Caught it at Rect.details2.."); } System.out.println("The 4 points of the Rectangle"); System.out.println(upperLeft.toString()); System.out.println(bottomRight.toString()); } public void addRect(Rectangle rect2){ try { upperLeft.setY(Math.max(upperLeft.getY(),rect2.upperLeft.getY())); upperLeft.setX(Math.min(upperLeft.getX(),rect2.upperLeft.getX())); bottomRight.setX(Math.max(bottomRight.getX(),rect2.bottomRight.getX())); bottomRight.setY(Math.min(bottomRight.getY(),rect2.bottomRight.getY())); } catch (NullPointerException e) { System.out.println("Caught it at addRect..."); } } } ================= public class TryRect extends Object { public static void main(String[] args) { System.out.println("Gutten Tag.."); Point p1 = new Point(20.5,34.5); Point p2 = new Point(21.5,56.5); Point p3 = new Point(26.5,86.5); Point p4 = new Point(28.5,98.5); Rectangle rect1 = new Rectangle(p1,p2); Rectangle rect2 = new Rectangle(p3,p4); rect1.details(); rect2.details(); rect1.addRect(rect2); rect1.details(); } }