I'm having a silly problem with a simple code that is driving me crazy:
cont_l = 0
@scroll2 = 0
@lot_sel = $lottatore
puts($lottatore[cont_l + @scroll2].write("engaged"))
y_lot = 206 + (cont_l * 30)
if @lot_sel == $lottatore[cont_l + @scroll2] then
color_text_1 = 0xff2DC800
elsif $lottatore[cont_l + @scroll2].write("engaged") == true then
color_text_1 = 0xffFF6600
color_text_1 = 0xffffffff
@font_Segoe_UI_30.draw_rel($lottatore[cont_l + @scroll2].write("nome"), 60, y_lot, 2, 0, 0.5, 1, 1, color_text_1)
cont_l = cont_l + 1
end until cont_l == @partita.output("tot_lot")
This returns this text:
#<NoMethodError: undefined method
write' for nil:NilClass>draw'
C:/Users/Alex/My Documents/Aptana Studio 3 Workspace/Wrestling Manager/main.rb:706:in
C:/Users/Alex/My Documents/Aptana Studio 3 Workspace/Wrestling Manager/main.rb:802:in
C:/Users/Alex/My Documents/Aptana Studio 3 Workspace/Wrestling Manager/main.rb:802:in `<main>'
So it appears that the $lottatore class doesn't exist, or that it hasn't got the "write" method.
Wich is not true.
As you see, I added a line to the code (the one you see in italic). Before the cycle begins, the program uses the same method of the same class that that cycle is using. And it returns the "false" line that i wrote in italic. Because that's it, $lottatore.write("engaged") is false. And also all the other objects in the array have their @engaged variable false.
So the problem is: why the program doesn't find the method called "write" during the cycle, if ten lines above it finds it?
Well, it looks like
$lottalore == nil. Two things come to my mind that could cause this:
1. You set this variable incorrectly somehow
2. You gave it its value in a different method (as the $ symbol indicates a temporary method)
Ok but then why it works with the puts? :\
Actually I'm on the iPhone. I'm preparing to go to the university. On the train or as soon as I can I want to try to put the puts AFTER the if. Just to see if it works even there, or if the conditional statement somehow resets the array.
tried. And the puts works after the "if". Wich means that the conditional doesn't reset the array.
I think I'll try to write that thing differently. Maybe it will work.
It looks like $lottatore# is returning nil -- if $lottatore itself was nil, then the error message would mention '' as the undefined method.
If $lottatore is an array, then it will return nil if you try to reference an element that's out of bounds. One way that could happen is if your loop's exit condition isn't being met before you've run out of elements -- it looks to me like you're relying on a side-effect to tell you when to end the loop, which is probably making the problem harder to debug.
Check your loop's exit condition, and since you're iterating over an array, you may want to consider re-writing this to use a for loop or the each method (or, in the very least, something that makes it clear what terminates the loop).
I think I've found out where the problem was.
First of all, "@tot_lot" didn't exist. I don't know why i didn't receive an error about that. The correct name of the variable was "@num_lot", wich anyway was initialized to 0, so even with that the loop would have been eternal.
So the condition for the loop to end was never met, and the @cont_l exceeded the number of elements in the array.
Beeing out of the loop, the puts returned a correct value of @engaged for the class $lottatore. When i inserted it inside the loop, it gave me a "false" output for every element in the array, until it reached the maximum number of values. Then, the error returned.
Now I have corrected the condition. I'll confirm you that everything is ok.
Not initialising an instance variable before using it is regarded as a warning, not an error. Try enabling warnings by passing ruby the -w option when you run your script.
Also, is there a reason why you're accessing instance variables like this rather than using Ruby's attributes or writing your own getters and setters?
I'm noob, that's the reason :( I learnt ruby by myself and I'm not a programmer, so my ruby knowledge is a bit, ehm, rough.
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill