NoMethodError when there really is no method

this code was built in a custom window inheriting from window-base
Spoiler: code
Code:
  #--------------------------------------------------------------------------
  # Calculations
  #--------------------------------------------------------------------------
  i = 0
  itemweight = 0
  loop do
    i += 1
    itemidweight = $data_items[i].weight
    itemidcount = $game_party.item_number($data_items[i])
    if itemidweight == 0
      next
    end
  
    itemtotalweight = itemidweight * itemidcount
    itemweight += itemtotalweight
    if i == 250
      break
    end
  
  end

it breaks on itemidweight = $data_items.weight and says that no method [] is found.

the .weight has been defined in another script.

Can anyone also explain why it says [] as a method?
what's the value of i at that point?

can you post the definition of weight?
Post the screenshot, as well as the whole script if possible.
A piece of code can't tell the whole thing.
Sorry.
Spoiler: Weight checker (in scripts)
Code:
class RPG::Item
  def weight
    if @weight.nil?
      weight_checker
      return @weight
    else
      return @weight
    end
  end
 
  def weight_checker
    @weight = self.note =~ /<Weight: (.*)>/i ? $1.to_i : 0
  end
end

Spoiler: The entire window
Code:
#==============================================================================
# ** Window_Weight
#------------------------------------------------------------------------------
#  This window shows weight. Or, at least I tried to.
#==============================================================================

class Window_Weight < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 144, fitting_height(2))
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_weight_value(value, weight_unit, 4, 0, contents.width - 8)
  end
 
  #--------------------------------------------------------------------------
  # Calculations
  #--------------------------------------------------------------------------
  i = 0
  itemweight = 0
  loop do
    i += 1
    itemidweight = $data_items[i].weight
    itemidcount = $game_party.item_number($data_items[i])
    if itemidweight == 0
      next
    end
 
    itemtotalweight = itemidweight * itemidcount
    itemweight += itemtotalweight
    if i == 250
      break
    end
 
  end
 
  #--------------------------------------------------------------------------
  # Get Weight Unit
  #--------------------------------------------------------------------------
  def weight_unit
    if itemweight >= 1000
      return "kg"
    else
      return "g"
    end
  end
 
  #--------------------------------------------------------------------------
  # Get Weight Unit
  #--------------------------------------------------------------------------
  def value
    if itemweight >= 1000
      value = itemweight / 1000
    else
      value = itemweight
    end
  end
 
  #--------------------------------------------------------------------------
  # * Open Window
  #--------------------------------------------------------------------------
  def open
    refresh
    super
  end
end
Spoiler: Other things to note (that I probably screwed up)
added definition for draw_weight_value in Window_Base
Code:
  #--------------------------------------------------------------------------
  # * Draw Weight with Unit
  #--------------------------------------------------------------------------
  def draw_weight_value(value, unit, x, y, width)
    cx = text_size(unit).width
    change_color(weight_color)
    draw_text(x, y, width - cx - 2, line_height, value, 2)
    change_color(weight_color)
    draw_text(x, y, width, line_height, unit, 2)
  end

another Window_Help has been made to fit the window (Width -144, x = 144)
"calculations" must be included in a procedure, like everything else.
and the definition of weight must be placed before the window.
As gstv87 said, your "calculations" are being evaluated when loading the scripts (i.e. before it even reached Main). $data_items is still undefined at this point (nil) and there is no such method nilClass#[]. I know in XP $data_items is defined when Scene_Title is running. I think it's the same in VXA.

Also the logic in your calculations is off. The iterator i can potentially be more than the number of items your game has. Your loop should be
Code:
$data_items.each do |item|
  next if item.nil?
  weight = item.weight
...
end
gstv87, KK20, thanks for your help. I just didn't know how to get the total number of items. How do you get the number of items that the party currently has, though? As currently I use the $game_party.item_number, but in the case of
Code:
$data_items.each do |item|
  next if item.nil?
  weight = item.weight
...
end
i do not know how to call that.
Code:
num = $game_party.item_number(item)
weight += num * item.weight

"|item|" is already an item class, pulled from $data_items
when you use that structure, you don't need to call $data_items[item.item_id], because "item" is already an item.
Thanks again.

Now when I run the script, it says that it can't find "+" for Nil:NilClass again.
I assume that when I defined 'weight' in the calculations, it doesn't allow the other defs to access it?
Code:
#==============================================================================
# ** Window_Weight
#------------------------------------------------------------------------------
#  This window shows weight. I tried, and failed. Twice.
#==============================================================================

class Window_Weight < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 144, fitting_height(2))
    weightcalc
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_weight_value(value, weight_unit, 4, 0, contents.width - 8)
  end
 
  #--------------------------------------------------------------------------
  # Calculations
  #--------------------------------------------------------------------------
  def weightcalc
    $data_items.each do |item|
      next if item.nil?
      itemidweight = item.weight
      itemidcount = $game_party.item_number(item)
      if itemidweight == 0 || itemidcount == 0
        next
      end
  
      itemtotalweight = itemidweight * itemidcount
      weight += itemtotalweight
    end
  end
 
  #--------------------------------------------------------------------------
  # Get Weight Unit
  #--------------------------------------------------------------------------
  def weight_unit
    if weight >= 1000
      return "kg"
    else
      return "g"
    end
  end
 
  #--------------------------------------------------------------------------
  # Get Value
  #--------------------------------------------------------------------------
  def value
    if weight >= 1000
      value = weight / 1000
    else
      value = weight
    end
  end
 
  #--------------------------------------------------------------------------
  # * Open Window
  #--------------------------------------------------------------------------
  def open
    refresh
    super
  end
end

Edit: With me defining 'weight' in 'weightcalc', it doesn't break for 'weight_unit' but on 'value'. Saying that it is undefined.

Edit 2: Swapping the order of 'value' and 'weight_unit' does nothing.
try
Code:
draw_weight_value(self.value, self.weight_unit, 4, 0, contents.width - 8)

and
Code:
def weightcalc
 @weight = 0
 $data_items.each do ...
 ...
 @weight += itemtotalweight
 end

and replace each occurence of "weight" (the counter), with "@weight"
Thanks! That worked completely fine. Can you explain why use "self"? I still don't understand it and I tend not to use it. Also, why set 'weight' as an instance variable (if I'm right) ?

Also asking. When I use class RPG::Item in the notetag reader, would I only be able to call only items, or items including weapons, key items, etc.?
self is for calling methods within the class you're working with. In your code, using value or self.value will result in the same thing: calling your value method. When it becomes critical is when it comes to "setters and getters" for your instance variables, such as making sure an actor's HP value is within acceptable bounds:
Code:
class Actor
...
  def hp
    @hp
  end

  def hp=(value)
    @hp = [[value, 0].max, @maxhp].min
  end
...
end
If you can imagine changing your actor's HP by doing either @hp = new_value versus self.hp = new_value, which is better?

weight is a local variable while @weight is an instance variable. Instance variables are persistent throughout the object's existence, so you can reference it from one method to another. Local variables within methods are discarded after the method returns.
So calling weight_calc will result in creating a local variable weight, having a value set to it, and then immediately purging it from memory when weight_calc finishes. By the time you reach value, you're creating an entirely new weight variable which has no value (nil). @weight will persist between method calls, so the value stored into it in weight_calc can be used in value.

Original thread: https://forums.rpgmakerweb.com/threads/nomethoderror-when-there-really-is-no-method.108334/