• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object receiving null values?  RSS feed

 
Ethan Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I run through the loop it comes up with all values being null, why is this? I checked before I added the values in that none of them were null

Ex:


And both player 1 and 2 values come up as the players name so I'm not sure why when it gets added to the arrayList it becomes null? Not sure if I'm doing something wrong here or....

Thanks,
Ethan
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are player1 and player2 and where are they declared and initialized?
 
Ethan Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:What are player1 and player2 and where are they declared and initialized?


Woops, forgot to add that in, this is using the bukkit API so when creating 'Player player1' is similar to using 'String player1' but its a little different for the Bukkit API.

Also in the command a little above where you add the object to the arrayList is when 'player' and 'requested' are initialized.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Smith wrote:When I run through the loop...

Ethan,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Thanks.

Winston
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you add another layer to the mystery, but it's still a mystery. Until you initialize WHATEVER player1 and player2 ultimately are or become, they will be null. Something like:

player = new Player();
requested = new Player();

both using the appropriate Player() constructor with the necessary arguments/parameters should exist in your program, or they will all be null.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Smith wrote:

If that is the literal flow of your program then that is the problem. When you assign the player1 and player2 parameters to the UserData object (UserData ud = new UserData(player1, player2, loc1, loc2)) the player1 and player2 variables have yet to be given a reference: they are null. So ud gets null players. Then later you assign objects to the two player variables. But because Java is always pass by value, not pass by reference, setting those two variables after sending nulls to ud does not change ud from having null values. You need to put non-null values into ud.
 
Ethan Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said in the OP, when I check the values of the players just before adding them to the list they are not null, and they appear as they should, but in the next line when they are added to the arrayList it somehow becomes null?

Although this should make no difference whatsoever this is what they are initialized. I purposly left this out since it uses the Bukkit API and my question is not about how to use the Bukkit API, its involving the actual java portion of it.


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's too much guessing about what you're doing/not doing, testing/not testing. If you can post an SSCCE that demonstrates what you say - first they're not null, then they're null - then post it. Otherwise, the guessing is getting us nowhere.
 
Ethan Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought my problem was clear enough....

I pass the player1 and player2 values into the object, I check the player values before I pass them into the object and neither are null. Then when I use the for loop to retrieve the values from the object they all appear null. My question is why is that? I don't understand why they are appearing null when they are not null when I added them to the object.

I also posted everything I was testing in the OP when I first made the thread so....
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Smith wrote:As I said in the OP, when I check the values of the players just before adding them to the list they are not null, and they appear as they should, but in the next line when they are added to the arrayList it somehow becomes null?

This is the code you showed in the original post:

You are printing out the players before the ud is added to the list. But that occurs after you put them into the ud. you should do:

And see what you get.


Although this should make no difference whatsoever this is what they are initialized. I purposly left this out since it uses the Bukkit API and my question is not about how to use the Bukkit API, its involving the actual java portion of it.



player and requested are inconsequential. What gets into ud is. Until we see an SSCCE I agree further troubleshooting is impossible.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Smith wrote:... I don't understand why they are appearing null when they are not null when I added them to the object.
You don't understand because you are making an untrue assumption somewhere. When we see functional code we can help.

I also posted everything I was testing in the OP when I first made the thread so....

Not true, that can't compile, and there is no way any variable in that original code would ever not be null. So you have more than that if you ever see non-null values (and are able to compile and run)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the closest thing to your code I could get that was an SSCCE:


It shows the same problem you describe, and I suspect it is for the same reason.

Ethan Smith wrote:...when they are added to the arrayList it somehow becomes null?

Nope.

The List has nothing to do with it.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You clearly use player1, player2 before assigning values to them.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:You clearly use player1, player2 before assigning values to them.

Exactly what I was telling the OP. The above code was my example to show that he can reproduce the problem, in SSCCE, without the Bukkit library, servers, or anything else, and that the problem isn't magic. It was made by copying his code as posted in the original post, then moving things and adding things until it compiled (but maintaining the order of the lines of code).
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know about the OP but I am persuaded :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!