Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Null Pointer when subtracting a Vector  (Read 963 times)
0 Members and 1 Guest are viewing this topic.
Offline kingAree

Senior Newbie


Exp: 4-6 months



« Posted 2017-04-26 10:05:01 »

Hi, Could anyone tell me why this following code gives me a NullPointerException?

1  
2  
3  
4  
5  
Vector2 e1 = vertices[i];
         
         Vector2 e2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
         
         Vector2 edge = e1.subtract(e2);

Vector2 Constructors and variables
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public float x, y;
   public Vector2(float x, float y){
      this.x = x;
      this.y = y;
   }
   
   public Vector2(Vector2 vec){
      this.x = vec.x;
      this.y = vec.y;

   }


Code to subtract vector
1  
2  
3  
4  
5  
public Vector2 subtract(Vector2 vec){
//NullPointerException at this point
      return new Vector2(x - vec.x, y - vec.y);

   }
Offline KaiHH

JGO Kernel


Medals: 442



« Reply #1 - Posted 2017-04-26 10:38:04 »

Have you heard of "debugging?"
If you use an IDE, you can set a breakpoint at the offending source line, start in debugging mode and inspect the values of all live arguments, variables and object fields when execution hits the breakpoint.
And then you'll find that the argument to the call of Vector2.subtract is null.
Offline kingAree

Senior Newbie


Exp: 4-6 months



« Reply #2 - Posted 2017-04-26 10:42:19 »

Hi, yeah I wasn't familiar with debugging until I had this problem and learned how to do it in Eclipse, it just took me to the Constructor of the Vector2 class, and then I couldn't really figure out the issue from there.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Kernel


Medals: 173



« Reply #3 - Posted 2017-04-26 12:53:02 »

You get a Nullpointer, if one of your variables contain a null value and you try to access a method or property on it. So in your case, at least one entry in your vertices array happen to be null.

Go on practicing using the debugger, because this kind of errors can be found easily with it. Hint: you can inspect the contents of variables in the current scope (frame) in a breakpoint, so searching for the origin of the null value should be a piece of cake, when familiar with debugging.

Mathias - I Know What [you] Did Last Summer!
Offline kingAree

Senior Newbie


Exp: 4-6 months



« Reply #4 - Posted 2017-04-26 18:15:23 »

Thanks for the advice! at the break-point instance nothing in my vertices array is equal to null, nor my subtract argument

Edit, never-mind, figured out how to use debugger with loops and yes there is definitely a null value, cheers.
Pages: [1]
  ignore  |  Print  
 
 

 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (143 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (623 views)
2017-04-27 17:45:51

buddyBro (732 views)
2017-04-05 03:38:00

CopyableCougar4 (1257 views)
2017-03-24 15:39:42

theagentd (1238 views)
2017-03-24 15:32:08

Rule (1215 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!