# String is not null, but it behaves as if it were

**URL:** <https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840>\
**Category:** Beginners\
**Created:** [September 6, 2019, 2:12pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840 "2019-09-06T14:12:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [September 6, 2019, 2:12pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/1 "2019-09-06T14:12:31Z")

</div>

Hi, i’m trying to read a ASCII object (.obj) file and i’m having troubles.  
In the file, there are some empty lines with only \r\n (CRLF) line breaks, for example, line 1 and 3 (the 1st line is 0).  
When I do things with the line (is a String), the line seems to be empty so, it has to be null (or “”), but is not null.  
Here is how to replicate

`void setup() {  
size(200, 200);  
String[] file = loadStrings(“sample.obj”); // Line 1 contains \r\n (CRLF)

if (file[1] == “\r\n”) { println(“Contains CRLF”); } // Or, not?

printArray(file[1].toCharArray()); // If I print the string as array, it seems like there is not any element, so it has to be null, right?

if (file[1] == null) { println(“Its sure that is null”); } // Well, no

if (file[1] == file[3]) { println(“1 & 3 are equal”); } // Even this is false

// What the hell is this?  
}`

The code dose’t print anything, even the printArray().

Here is my .obj file: [https://drive.google.com/file/d/1Fu2xcDPLcdZIXM8VFoEiR3Bx-5cZTlTA/view?usp=sharing](https://drive.google.com/file/d/1Fu2xcDPLcdZIXM8VFoEiR3Bx-5cZTlTA/view?usp=sharing)

Any one can help me?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2019, 2:25pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/2 "2019-09-06T14:25:58Z")

</div>

> [@AngelRF](#):
>
> if (file[1] == “\r\n”)

first line would be `file[0]`

---

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [September 6, 2019, 2:29pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/3 "2019-09-06T14:29:09Z")

</div>

You are right, but the first line is not empty, is the line 1 and 3, that are empty  
The problem is only by the empty lines so i’m using line 1 (second line).

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 6, 2019, 2:31pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/4 "2019-09-06T14:31:36Z")

</div>

The Processing statement `loadStrings` will load an ASCII file into a String array. Some of the array elements maybe empty Strings `""` but _ **none** _ of the array elements will be `null`

So `(file[1] == null)` will never be true.

> [@AngelRF](#):
>
> `if (file[1] == file[3]) { println(“1 & 3 are equal”); } // Even this is false`

Strings are objects so this statement asks if they are the same object and clearly they aren’t because each element in the array is a different object. To compare the characters in these objects use  
`if (file[1].equals(file[3])) { println(“1 & 3 are equal”); } `  
or  
`if (file[1].equalsIgnoreCase(file[3])) { println(“1 & 3 are equal”); } `

---

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [September 6, 2019, 2:35pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/5 "2019-09-06T14:35:16Z")

</div>

Thanks for your answer, but `(file[1] == "")` is false too

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2019, 2:37pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/6 "2019-09-06T14:37:30Z")

</div>

> [@AngelRF](#):
>
> `(file[1] == "")`

You can’t use `==` with `String` (as _quark_ mentioned).

Instead use: `if ( file[1].equals("") )`

---

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [September 6, 2019, 2:40pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/7 "2019-09-06T14:40:29Z")

</div>

Ok, so much thanks for your help, excuse me, I’m still a beginner

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 6, 2019, 2:47pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/8 "2019-09-06T14:47:46Z")

</div>

> [@Chrisir](#):
>
> Instead use: `if ( file[1].equals("") )`

In order to avoid a potential `null` value, it’s much safer to test `""` (or any String literal) against a variable when using [**equals()**](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)): 🤓  
`if ( "".equals(variable) ) {}`

Or better yet, use method [**isEmpty()**](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isEmpty()): 💡  
`if ( variable.isEmpty() ) {}`

Or alternatively, method **isBlank()** can some times be better than vanilla [**isEmpty()**](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isEmpty()):  
`if ( variable.isBlank() ) {}`

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank())

---

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [September 6, 2019, 2:49pm UTC](https://discourse.processing.org/t/string-is-not-null-but-it-behaves-as-if-it-were/13840/9 "2019-09-06T14:49:39Z")

</div>

I’ll take that into account, thanks 😁
