NullPointerException error

Hello guys,

Based on what i understand, NullPointerException error is happening when some value is not initialize yet and we try to call it in a function. But looking at my code, I tried to re-initialize the values in NullPointerException error line but it seems that the error are still there. Can anyone help me with this?

Tagged:

Answers

  • sure...

    sometimes multiple values in one line can be suspicious candidates, maybe you're looking at the wrong one?

    in the line before your error line say

    if (array1==null) 
         println ("array1 is the bad guy");
    

    or post your code....

  • I see

    which line holds the error?

  • can i give u a link to my code? it kinda long

  • in float distance = dist(ballPos.x, ballPos.y, tempBall.ballPos.x, tempBall.ballPos.y);

    this is your problem:

    tempBall.ballPos

    tempBall itself is ok though

    since tempBall is coming from table.b_arr[i]; make sure tempBall.ballPos is set there

  • aaaaaaaaaaaaa

    the same error come out when i tried to set it up

  • edited December 2016

    is that your code??

    you cannot just reset it there!!!

  • aaaaaaaaaaaaa

    but i set it in my Ball class. i have no idea where should i reset it. im really new with computing, im sorry

  • edited December 2016

    ok, several issues

    first

    first don't call repel in the constructor of class Ball (line 35) !!!!!!!

    2nd

    2nd in class Table you say

        Ball [] b_arr = new Ball[16];
    

    16 balls, ok, but in get_array_ball you define only 14 balls, so it crashes with number 15 (say 14 in class Table)

    at the moment, everywhere it says < table.b_arr.length in a for-loop it will crash

  • i editted the array size to 14.

    so, where should i call it? actually, i modify the code from this pde file,

    https://www.openprocessing.org/sketch/126036

    to make it fit my program. i just change the array and the object in the array. in that code, they call repel in the class Ball.

  • yeah, it works in the original code because they use an arrayList instead of a fixed array as you have

    just don't call repel at all for now.

    (Repel has the function to make sure that the balls don't overlap each other in the beginning. Not important.)

  • okay, i got it. thanks mate. appreciate your help a lot. now i can proceed with it. :D

Sign In or Register to comment.