# MethodError: no method matching -(::VariableRef, ::Nothing)

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-variableref-nothing/35126>\
**Category:** General Usage\
**Tags:** question, jump, error\
**Created:** [February 25, 2020, 5:24pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-variableref-nothing/35126 "2020-02-25T17:24:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alkin](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@alkin](https://discourse.julialang.org/u/alkin)\
**Post date:** [February 25, 2020, 5:24pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-variableref-nothing/35126/1 "2020-02-25T17:24:26Z")

</div>

I am new to julia and I am still trying to learn about it.

I tried to use a user-defined function in my constraint.

Basically, the lookup function returns the value of the same index as the input in the array:

```
function lookup(scenario,zw_set,sn_set)
for i= 1:length(sn_set)
    if sn_set[i] == scenario
            return zw_set[i]
        end
    end
end

```

Thus I tried to apply this function in my optimization model:

```
m =Model(with_optimizer(CPLEX.Optimizer))
T = map(Int64, collect(1:1:5))
R = [1; 2]
@variable(m, sn[R,T] >=0,Int)
@variable(m,zw1[T]>=0, Int)
@constraint(m, [t in T], zw1[t]==lookup(sn[1,t],w1,snx1))

```

and I got this error:

```
   ERROR: LoadError: MethodError: no method matching -(::VariableRef, ::Nothing)
Closest candidates are:
  -(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at C:\Users\alkin\.julia\packages\MathOptInterface\DmQBj\src\Utilities\functions.jl:1125
  -(::AbstractVariableRef) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:58
  -(::AbstractVariableRef, ::Union{Number, LinearAlgebra.UniformScaling}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:62
  ...
Stacktrace:
 [1] sub_mul(::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\MutableArithmetics.jl:31
 [2] operate(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:88
 [3] operate_fallback!(::MutableArithmetics.NotMutable, ::Function, ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:275
 [4] operate!(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:67
 [5] macro expansion at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:224 [inlined]
 [6] macro expansion at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:380 [inlined]
 [7] (::var"#5653#5654")(::Int64) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\macro.jl:183
 [8] #28 at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85 [inlined]
 [9] iterate at .\generator.jl:47 [inlined]
 [10] collect(::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}},JuMP.Containers.var"#28#29"{var"#5653#5654"}}) at .\array.jl:622
 [11] map(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at .\abstractarray.jl:2099
 [12] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}, ::Type{JuMP.Containers.DenseAxisArray}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85    
 [13] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:65
 [14] top-level scope at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:45

```

w1 and snx1 has the following data:

```
julia> print(w1)
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 0.0, 1.0]

julia> print(snx1)
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0]

```

I realized that it might have to do something with the data types and array stuff?

Please help

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 26, 2020, 2:42pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-variableref-nothing/35126/2 "2020-02-26T14:42:51Z")

</div>

Take a read of the first post in [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

It’s easier to help if you can provide a minimal working example that demonstrates the problem.

The main reason is that your function `lookup` is returning `nothing`. However, the bigger issue is that you cannot use functions like this in a JuMP model. In particular, you are passing the variable `sn[1, t]`, **not** the value of the variable.

You will need to formulate the problem as a mixed integer linear program.

---

<div class="post-metadata">

**Author:** ![alkin](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@alkin](https://discourse.julialang.org/u/alkin)\
**Post date:** [March 3, 2020, 12:36pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-variableref-nothing/35126/3 "2020-03-03T12:36:35Z")

</div>

I see, then I need to formulate this problem into a model.

thank you!
