Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By gimbal2
  • 1 Post By gimbal2
  • 1 Post By gimbal2

Thread: How to solve java.lang.NullPointerException error?

  1. #1
    aschroft is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default How to solve java.lang.NullPointerException error?

    I am getting this error Exception in thread "main" java.lang.NullPointerException on these line:

    1) points[k].set(xValues[k], yValues[k]);

    2) if(!setPoints(numPoints, xArray, yArray))

    3) Polygon Polygon1 = new Polygon(4, xArray1, yArray1);

    From what I analyze, the second (in the constructor) and the third (in main) one originated from the first one which is a snippet from the setPoints method since they are both using setPoints method inside.

    I wonder what did I do wrong in the method when I assign new X and Y values to the polygon, and I don't think I am passing any null variable to the points[k] that can cause this error because when I tried to print xValues[k] and yValues[k] I can't find any null variable.

    Here is the snippet of the method:

    public Boolean setPoints(int numPoints, float xValues[], float yValues[])
    {
    if( numPoints < 0 || numPoints > MAX_POINTS ||
    xValues.length < numPoints || yValues.length < numPoints)
    return false;

    this.numPoints = numPoints;
    points = new Point[numPoints];

    for (int k = 0; k <= numPoints-1; k++)
    points[k].set( xValues[k], yValues[k] );

    return true;
    }

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

    Default Re: How to solve java.lang.NullPointerException error?

    points[k] contains a null value, so trying to invoke a set() method on whatever is in there is going to result in a NullPointerException.

    Lesson to learn: creating an array of objects does NOT create instances of those objects in each index of the array. God I can remember when I was in school and I was running into this exact problem, when I finally realized the problem after a few days of blaming Java for all the problems in the world I had a red hand print on my forehead for about a week after the epic facepalm I did.
    aschroft likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    aschroft is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to solve java.lang.NullPointerException error?

    If that so, how can I pass the values from xValues[k] and yValues[k] to the array points[k] safely ?
    This method setPoints was meant to change the points array instances with the value we are passing from xValues array and yValues array.
    Do I have to limit the index I am changing so that I won't encounter the null instances ?
    Or should I create the instances of those objects in each index by using a for loop ?
    Yeah this is such an annoying error for someone new in programming, hopefully I can solve this soon enough.

    Quote Originally Posted by gimbal2 View Post
    points[k] contains a null value, so trying to invoke a set() method on whatever is in there is going to result in a NullPointerException.

    Lesson to learn: creating an array of objects does NOT create instances of those objects in each index of the array. God I can remember when I was in school and I was running into this exact problem, when I finally realized the problem after a few days of blaming Java for all the problems in the world I had a red hand print on my forehead for about a week after the epic facepalm I did.
    Last edited by aschroft; 12-04-2013 at 01:55 PM.

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

    Default Re: How to solve java.lang.NullPointerException error?

    You have nothing to transfer the values TO yet; that's the whole problem. You want a Point object instance in each array index on which you can invoke the set() method. So you need to create one yourself.

    Imagine an Ikea bookcase that you have to put together yourself; so far you've constructed the frame, but you have not put in the shelves yet so anything you're putting into the bookcase is falling to the floor. The NullPointerException is the "thud" sound the book makes.
    aschroft likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    aschroft is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to solve java.lang.NullPointerException error?

    Will something like this solve the problem ?

    for (int k = 0; k <= numPoints-1; k++)
    points[k] = new Point();

    It worked, but the values won't pass from the xValues & yValues to the points array and what I'm getting from the point array are all default values.
    Here comes another problem after the other
    Thanks for the help though, such a good illustration from you.
    Last edited by aschroft; 12-04-2013 at 02:11 PM.

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

    Default Re: How to solve java.lang.NullPointerException error?

    You got it!

    As for your next problem: you're still calling that set() method aren't you? Not that you really need it, I'm pretty sure that Point class you're using has a specific constructor that takes an x and a y value.
    aschroft likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    aschroft is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to solve java.lang.NullPointerException error?

    My bad, it turns out I used another for loop to re-initialize the objects in my display method and causing all my values to be the default values again.
    Thanks for the help, finished the assignment and I can sleep now
    Quote Originally Posted by gimbal2 View Post
    You got it!

    As for your next problem: you're still calling that set() method aren't you? Not that you really need it, I'm pretty sure that Point class you're using has a specific constructor that takes an x and a y value.
    Last edited by aschroft; 12-04-2013 at 02:39 PM.

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Please solve my error occured java.lang.NullPointerException
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-24-2010, 12:08 PM
  3. Please solve my java.lang.NullPointerException error.?
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-23-2010, 09:30 PM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 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
  •