java.lang.NullPointerException
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: java.lang.NullPointerException

  1. #1
    Join Date
    Dec 2011
    Posts
    2

    java.lang.NullPointerException

    hi, I am creating a class and trying to create an array of that class, then giving value to the variables contained in the class but get the infamous NullPointerException. How can I give a value to the values of the variables in the class (in this case x and y)?
    Thanks

    This is my code:
    class Point {
    int x, y;
    }

    class Test {
    public static void main(String[] args) {
    Point[] pa = new Point[10];
    pa[1].x=1;
    }
    }

    This is the output:
    Exception in thread "main" java.lang.NullPointerException
    at Test.main(test.java:8)

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.lang.NullPointerException

    You have defined an array of 10 Point objects but you haven't created any Point objects to put in the array hence the element at array index 1 is null and not a Point object.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2011
    Posts
    2

    Re: java.lang.NullPointerException

    ohhh thanks dude, you saved my final project!!!

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center