Page 1 of 3 123 LastLast
Results 1 to 20 of 47
Like Tree6Likes

Thread: not marked as wrong, but when trying to run, displays exception?

  1. #1
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Post not marked as wrong, but when trying to run, displays exception?

    this might look long but, its very repetitive and easy to understand. when i try to run it, it stops and says something about an exception. the error is commented out in the code. thank you for your time :)
    Java Code:
     public class Shapes {
    
        public static class Point {
    
            protected int x;
            protected int y;
    
            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
    
        public abstract static class Shape {
    
            protected int x;
            protected int y;
    
            //public void Move(int x, int y) {
                //this.x = x;
                //this.y = y;
            //}
    
            public abstract void Show();
    
            static class Line extends Shapes {
    
                Point a;
                Point b;
    
                public Point GetA() {
                    return a;
                }
    
                public void SetA(Point a) {
                    this.a = a;
                }
    
                public Point GetB() {
                    return b;
                }
    
                public void SetB(Point b) {
                    this.b = b;
                }
    
                public void Show() {
                    System.out.println("this is a line");
                }
    
                public String toString() {
                    String toString = "Point a is" + a.x + b.y + "Point b is" + a.x + b.y;
                    return toString;
                }
            }
    
            static class Circle extends Shapes {
    
                Point center;
                double radius;
    
                public Point GetCenter() {
                    return center;
                }
    
                public void SetCenter(Point center) {
                    this.center = center;
                }
    
                public double GetRadius() {
                    return radius;
                }
    
                public void SetRadius(double radius) {
                    this.radius = radius;
                }
    
                public void Show() {
                    System.out.println("this is a circle");
                }
    
                public String toString() {
                    String toString = "the center point is" + center.x + center.y + "the radius is" + this.radius;
                    return toString;
                }
            }
    
            static class Rectangle extends Shapes {
    
                Point a;
                Point b;
    
                public Point GetA() {
                    return a;
                }
    
                public void SetA(Point a) {
                    this.a = a;
                }
    
                public Point GetB() {
                    return b;
                }
    
                public void SetB(Point b) {
                    this.b = b;
                }
    
                public void Show() {
                    System.out.println("this is a rectangle");
                }
    
                public String toString() {
                    String toString = "Point a is" + a.x + b.y + "Point b is" + a.x + b.y;
                    return toString;
                }
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            Point pointA = new Point(5,6);
            Point pointB = new Point(8,10);
            Point center = new Point(0,0);
            double radius = 8.0;
            
            Shape.Rectangle rectangle = new Shapes.Shape.Rectangle();
            rectangle.Show();
            rectangle.toString(); // THIS IS THE ERROR!!!!!!!! SOMETHING ABOUT THIS IS WRONG.
            Shape.Circle circle = new Shapes.Shape.Circle();
            circle.Show();
    
            Shape.Line line = new Shapes.Shape.Line();
            line.Show();
        }
    }
    THIS IS WHAT HAPPENS WHEN RAN:
    this is a rectangle
    Exception in thread "main" java.lang.NullPointerException
    at Shapes.Shapes$Shape$Rectangle.toString(Shapes.java :124)
    at Shapes.Shapes.main(Shapes.java:139)
    Java Result: 1

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: not marked as wrong, but when trying to run, displays exception?

    In your toString method, you have a and b points which are not initialized and therefore null

  3. #3
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    didn't i already do that with the PointA and PointB in the main?? i am a beginner so bear with me here ^^

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: not marked as wrong, but when trying to run, displays exception?

    No, you just created three Point objects that have no use (yet).
    Maybe you want to pass these point objects to the rectangle's data fields so that a and b of rectangle be references to these objects you created
    or provide with default values so that a and b are never null in the first place.
    As I see it now, you can use rectangle's set methods to set a, b to the ones you created and then use the methods.

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    you have create Two Point Object inside the Main method but these two Point objects has nothing to do with Point a andPoint b inside the Rectangle Class.. you have two options here.. you can pass the two Point Object - which has been created inside the main method - to the Constructor or Rectangle Class or you can use the SetA and SetB methods of the Rectangle Class to initialize the Point Objects..




    superhaNds you are fast :P

  6. #6
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    i added rectangle.SetA(pointA); rectangle.SetB(pointB); right after rectangle.show(); , but how do u make it display the points i gave it? i mean i just set those points.....

  7. #7
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    by Changing the line 129 : rectangle.toString(); into --> System.out.println(rectangle.toString());
    Last edited by ShadowWalker; 07-23-2013 at 03:00 AM.

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by ShadowWalker View Post
    superhaNds you are fast :P

    Thus my nickname ;)
    ShadowWalker likes this.

  9. #9
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    lol, that was noob of me. thanks guys. ill keep working on this! when i run into problems later, i guess superhaNds will be here to help! and ShadowWalker too!
    ShadowWalker likes this.

  10. #10
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by HelpingIsCaring View Post
    lol, that was noob of me. thanks guys. ill keep working on this! when i run into problems later, i guess superhaNds will be here to help! and ShadowWalker too!
    Exactly; here we will be, deep in the summer helping people debug in Java instead of taking a holiday or something.
    My life should be a sad meme in 9gag

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by superhaNds View Post
    Exactly; here we will be, deep in the summer helping people debug in Java instead of taking a holiday or something.
    My life should be a sad meme in 9gag
    Or do both at the same time: I'm sitting in bright sunlight with my laptop, reading this forum. The disadvantage is that I can't read the postings very well bcause of the sunlight, but maybe it's an advantage so that I can keep my mouth shut, sipping my espresso near the fishes in my pond, waiting until my Grolsch is cold enough, program a bit, sleep a bit, smoke my tobacco ...

    Life can be good if only that stupid http server (running on a small micro controller) would be a bit faster and the http protocol wouldn't be that stupid ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: not marked as wrong, but when trying to run, displays exception?

    ...

    and there I am in a stuffy office. I hate you a little right now JosAH.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by gimbal2 View Post
    ...

    and there I am in a stuffy office. I hate you a little right now JosAH.
    How strange because I don't hate myself at all; especially not becaue I just discovered a highly efficient method to win that stupid 'Mahjong Titans' game: simply press the 'H' key and the 'enter' key in rapid succession until you win or no more moves can be made. If only I could speed up that stupid Chinese module that takes care of the 802.11b/g to/from serial communication ... my micro controller is waiting 90% of its duty cycle ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Guys go to the Beach!! :P enjoy the holidays..or visit Greece and you will feel what Summer means

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: not marked as wrong, but when trying to run, displays exception?

    I once wrote a bot that used java.awt.Robot to both read the screen and click the tiles to solve Minesweeper expert level in 2 seconds ... then Win7 (never used Vista, maybe that's where the change happened) made Minesweeper scalable and all the colors changed :(
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by ShadowWalker View Post
    Guys go to the Beach!! :P enjoy the holidays..or visit Greece and you will feel what Summer means
    Monsoon here, summer was over a month and half ago :)
    ShadowWalker likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by ShadowWalker View Post
    Guys go to the Beach!! :P enjoy the holidays..or visit Greece and you will feel what Summer means
    I wish you were the president. I don't know of what, but I would like you in a leadership position right now.
    ShadowWalker and DarrylBurke like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Same here...;) and almost a month and a half yet.. :P

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Quote Originally Posted by ShadowWalker View Post
    Guys go to the Beach!! :P enjoy the holidays..or visit Greece and you will feel what Summer means
    Not overhere thank you; this is what the beaches are like:

    'De kans op echt koelere dagen is kleiner geworden' - Het weer - VK
    In Arcen (Limburg) is het pas heet: 34 graden - Het weer - VK

    I prefer my garden thank you ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: not marked as wrong, but when trying to run, displays exception?

    Well that depends on the beach you go to, I know some sneaky beaches in Zeeland for example that not so many people know about ;) Of course getting there is still a bit a nightmare.

    Now imagine a beach in Greece - pure blue water, gold sand, empty beach (because everyone has to work double shifts just to pay the bills) and a snowy mountain on the horizon.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 3 123 LastLast

Similar Threads

  1. Exception name for wrong return type?
    By kkid in forum New To Java
    Replies: 2
    Last Post: 11-09-2012, 06:19 PM
  2. java.io.IOException: Stream not marked
    By joelBeaton in forum Networking
    Replies: 9
    Last Post: 04-03-2012, 08:07 AM
  3. Problems with getTables marked
    By albyblueska in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 08:20 PM
  4. Error Eclipse &drjava displays wrong language
    By fenderman in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •