Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default I'm lost, totally(pointer exceptions?)

    I made this program:
    Java Code:
    class Triangle
    {
    double area;
    int height;
    int length;
    public static void main(String [] args) 
    {
    int x = 0;
    Triangle[] ta = new Triangle[4];
    
    
    while(x<4)
    {
    ta[x] = new Triangle();
    ta[x].height = ((x + 1) * 2);
    ta[x].length = x + 4;
    ta[x].setArea();
    System.out.print("triangle "+x+", area");
    System.out.println(" = " + ta[x].area);
    x = x + 1;
    }
    int y = x;
    x = 27;
    Triangle t5 = ta[2];
    ta[2].area = 343;
    System.out.print("y = " + y);
    System.out.println(", t5 area = "+ t5.area);
    }
    
    void setArea()
    {
    area = (height * length) / 2;
    }
    }

    the error message:

    Exception in thread "main" java.lang.NullPointerException
    at Triangle.main(Triangle.java:16)


    extra notes:
    I've noticed that when I change the line of the code it will still say that error on the same line(even know dif line of code).

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Which is line 16?

    (By the way, I can't see any reason for any variable to be null in that code.)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    really.... how could that be...

    i save it... well thanks guys

    i'll figure it out...

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    6

    Default

    use
    x++;
    instead of x=x+1;
    :)
    (won't solve your problem, is just cleaner)

Similar Threads

  1. Drag & Drop, invalidDnDException, totally urgent.
    By Mikey_S in forum AWT / Swing
    Replies: 0
    Last Post: 07-05-2009, 12:56 PM
  2. I'm lost :(
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 03-03-2009, 05:18 AM
  3. So Lost
    By kandt in forum New To Java
    Replies: 5
    Last Post: 12-13-2008, 10:55 PM
  4. [SOLVED] Null Pointer Exceptions
    By Riftwalker in forum Advanced Java
    Replies: 4
    Last Post: 09-26-2008, 04:19 PM
  5. Replies: 1
    Last Post: 09-04-2008, 10:07 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
  •