aspose file tools*
The moose likes Groovy and the fly likes Problem: null elements in a Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Problem: null elements in a Vector" Watch "Problem: null elements in a Vector" New topic
Author

Problem: null elements in a Vector

Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

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

Joined: Aug 23, 2010
Posts: 28
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You'll probably need to show us the add() method from the Vector class.
Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Aug 23, 2010
Posts: 28
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Aug 23, 2010
Posts: 28
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Aug 23, 2010
Posts: 28
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem: null elements in a Vector