• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem: null elements in a Vector

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm writing a script in Groovy -my first time-. The synthesized code where I'm getting the error is that:




Where Player is a class (also written in java) containing some variables (pos, home_x, home_y...) and methods (the most important: set_player(), that sets the values from its args into the member variables).

set_player() is working OK, beacause I'm sure that the aux object contains all the values correctly. But the problem is with the add() method. aux object isn't copied ok, and when the "switch" line is executed, I get the next error: exception from Groovy: java.lang.NullPointerException: Cannot get property: pos on null object.

I've tried also with other methods like add(index, object) and also set(index, object) but it's always the same. add() is returning true, so I'm really disoriented.

I'll be very grateful for any help or advice!! I'm very hurry, I have to finish it early! My objective isn't to get an strictly correct Groovy code, I only need that it works!

Hernán.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is set_player() in the Player class returning? Because you're adding whatever that returns to the Vector, not the aux object itself. I'd guess that's where the problem is.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool! Thanks for your fast reply! It was the problem!

Now I'm getting some other errors, I have to continue debugging the script, maybe I will need your help another time.

Thanks,
Hernán.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slight nitpick...

In Java/Groovy method names are generally camel cased. So instead of set_team_name() it would be setTeamName(). There are other benefits to sticking with the standard that I'll not get into for now.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg, yes, it's my first program in Java/Groovy; I'm learning these "implicit" rules as I write code, I come from C and C++!! xD

Well, now my problem is this. The result of the sequence of add() methods:



is a vector with the same object at all the 10 positions (I'm not sure if the object is the latest I add). Why is happening this?

Hernán.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll probably need to show us the add() method from the Vector class.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Vector is member of the java.util package. The description of the add() method is:

add(Object o)
Appends the specified element to the end of this Vector.

There is another implementation of this method: add(int index, Object o) especifying the index where you want add the object. They exist the addElement() and the set() methods, too, that they do similar work.

You have more info here: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:Class Vector is member of the java.util package. The description of the add() method is:

add(Object o)
Appends the specified element to the end of this Vector.

There is another implementation of this method: add(int index, Object o) especifying the index where you want add the object. They exist the addElement() and the set() methods, too, that they do similar work.

You have more info here: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html



Doh! Missed that. Sorry. So aside from the big question of why are you using a Vector (unless you require synchronization, it's going to be way slower than a List or Set), the following confuses me. Maybe you can explain...



You call add and in that method you call aux.set_player(....). Generally, a set method doesn't return anything. Does yours? If so, what does it return?
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, my set_player() function wasn't returning anything, but the other problem I solved it by returning the object. I will explain this a little better showing you the set_player() code:



Something like this (simplifying). So my set_player() function is a kind of mixing a set method and a get method . I know it isn't the most correct, but now I haven't much time.

So, aux is a Player object that changes its content each time I call set_player() with different arguments. The problem is that I add a new aux each time, but after calling 10 times the add() method, I get the same object in all the 10 positions. I think it's the latest object I add to the vector.

Hernán.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you're set method is always acting on a single Player object, which you instantiated before you start adding to the Vector. So you are just replacing values in Player with new ones each time. Hence, why you see the same player with the same data in every position. You'll need to change your code to do something like this...



 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it was the problem. I did the most "elegant" way, as a constructor. And about the Vector class, I paid attention to you, and I changed it to an ArrayList class (List doesn't work beacause is an abstract class).
Even I changed the name of set_player() to SetPlayer() !!

Thanks a lot!!
Hernán.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:Yes, it was the problem. I did the most "elegant" way, as a constructor. And about the Vector class, I paid attention to you, and I changed it to an ArrayList class (List doesn't work beacause is an abstract class).
Even I changed the name of set_player() to SetPlayer() !!

Thanks a lot!!
Hernán.



Awesome! List is actually an Interface, to be specific. Generally, what you would do is act on the Interface which would be instantiated as a particular implementation. For example...



In Groovy, it's even easier, because you just don't really need to worry about it...



And if you want to make sure you use a specific implementation...


 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, an interface, sorry ;). I wrote a similar sentence than your first code line for instatiate the ArrayList.

I don't believe that you know this, but anyway I'm going to ask it:
You know something about GEVA? (Grammatical Evolution in Java) or, if not, you know anybody that can help me with this?

Hernán.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:Yes, an interface, sorry ;). I wrote a similar sentence than your first code line for instatiate the ArrayList.

I don't believe that you know this, but anyway I'm going to ask it:
You know something about GEVA? (Grammatical Evolution in Java) or, if not, you know anybody that can help me with this?

Hernán.



No, sorry.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic