# Assigning a null value to a variable

**URL:** <https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988>\
**Category:** General Usage\
**Created:** [March 12, 2021, 1:22am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988 "2021-03-12T01:22:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 1:22am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/1 "2021-03-12T01:22:28Z")

</div>

Hi,

I am trying to write a variable x to a matrix, that is output ultimately as a CSV file. Sometimes x will have a numerical value and other time no value (a null in my old language VBA). The question is how to assign a null value to x. I downloaded Missings.jl that has the “nothing” value, but this doesn’t work (see code and output below). There must be something very basic I am missing

Thanks Peter

```julia
import Pkg; Pkg.add("Missings")

for i = 1:10

    if i < 5
        x = nothing
    else
        x=1
    end
@show x
end

x = nothing
x = nothing
x = nothing
x = nothing
x = 1
x = 1
x = 1
x = 1
x = 1
x = 1

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [March 12, 2021, 1:31am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/2 "2021-03-12T01:31:01Z")

</div>

I’m pretty unsure what you expected would happen here. Can you clarify?

Note that you maybe want the value `missing`, not `nothing`, if you’re using Missings.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 1:45am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/3 "2021-03-12T01:45:30Z")

</div>

Hi,

Thanks for quick response. I tried missing and got same result. What I want is for x = null (just a blank empty space) when i \< 5. I hope this is clear.

Thanks Peter

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 12, 2021, 2:29am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/4 "2021-03-12T02:29:03Z")

</div>

First things first - both `nothing` and `missing` are available without using the Missings.jl package. In julia, these two values are semantically different (I don’t have the link, but there’s a great GitHub issue describing them as “the software engineer’s null” and “the data scientist’s” null or something).

One example of how they’re different is that `1 + missing` is `missing` where `1 + nothing` throws an error.

None of this exactly answers your question, I think your question may be arising from some misunderstanding. `x = nothing` in fact assigns the variable `x` to the value `nothing`. Perhaps it’s just that you’re using `@show` instead of `println()`?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 12, 2021, 2:33am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/5 "2021-03-12T02:33:23Z")

</div>

I should also mention the excellent `CSV.jl` package if you’re going to be writing csvs from matrices, and that package will deal correctly with `missing` for sure (those will be empty by default, but there’s an option to specify). It might deal with `nothing` the same way, though I don’t know that for sure.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 2:42am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/6 "2021-03-12T02:42:02Z")

</div>

Kev,

Thanks for response. I tried println() but same result. In my actual program I assign “x” to a matrix location, then make a DataFrame from the matrix and then use CSV,write(…) command to make the CSV file. Maybe I am missing an option in the the CSV.write arguments … need to explore further.  
Thanks Peter

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 4:17am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/7 "2021-03-12T04:17:42Z")

</div>

I forgot to mention that my matrix in my actual program is initialized with zeros, so it probably doesn’t accept values that aren’t numbers. Is there a way to define the matrix to accept both real numbers and blanks(null vamue)?

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 5:37am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/8 "2021-03-12T05:37:04Z")

</div>

I found below works. Good old trial and error. I am actual program I had the declare my array as Any as below and then assigned the element ’ ’ when I wanted to write/print a blank. Hope it is useful to someone else.

```julia
 Matrix_Detailed_GH = Array{Any}(missing, Hours_Total,182) # actual matrix

# write a null
import Pkg; Pkg.add("Missings")

for i = 1:10

    if i < 5
        x = ' '
    else
        x=1
    end
println(x)
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 12, 2021, 6:04am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/9 "2021-03-12T06:04:03Z")

</div>

> [@Peter-Jolly](#):
>
> What I want is for x = null (just a blank empty space)

There is no predefined value called `null` in Julia, so it is not clear what you want here. Rather than using `' '` for missing values, `missing` or `nothing` (depending on the semantics you need) would allow you to interface with more functionality in the ecosystem.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 12, 2021, 6:28am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/10 "2021-03-12T06:28:53Z")

</div>

> [@Peter-Jolly](#):
>
> Is there a way to define the matrix to accept both real numbers and blanks(null vamue)?

Yes, `allowmissings` function does that, eg. `allowmissings(zeros(10))`.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [March 12, 2021, 9:14am UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/11 "2021-03-12T09:14:29Z")

</div>

Thank you all for the responses. I now have a couple of options. Peter

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 12, 2021, 12:06pm UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/12 "2021-03-12T12:06:44Z")

</div>

> [@Peter-Jolly](#):
>
> I forgot to mention that my matrix in my actual program is initialized with zeros, so it probably doesn’t accept values that aren’t numbers.

Another option if your matrix is of Float64 would be to use `NaN`, though I’m not sure how CSV.jl freaks with those

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 12, 2021, 3:32pm UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/13 "2021-03-12T15:32:35Z")

</div>

CSV.jl is world class. It can handle anything!

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 12, 2021, 3:44pm UTC](https://discourse.julialang.org/t/assigning-a-null-value-to-a-variable/56988/14 "2021-03-12T15:44:09Z")

</div>

Agree completely! _Can_ handle for sure, I just don’t know if they’re handled by default the way @Peter-Jolly wants.
