Null Pointer Exceptions In Java - What EXACTLY They Are and How to Fix Them

Null Pointer Exceptions In Java - What EXACTLY They Are and How to Fix Them

Coding with John

2 года назад

90,520 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@user-up9lf5rc5p
@user-up9lf5rc5p - 10.12.2023 01:24

useless video, wasted time

Ответить
@VishalSharma-rn7mt
@VishalSharma-rn7mt - 28.11.2023 06:08

Awesome

Ответить
@UmpikLumpik
@UmpikLumpik - 01.08.2023 12:51

Nothing and no-one is equal to Newman 🤣

Ответить
@gahnendesgnu9012
@gahnendesgnu9012 - 16.05.2023 22:06

Hi John, thanks for the brilliant video.

Ответить
@lethality3704
@lethality3704 - 07.04.2023 15:58

Hi John, how useful are JML, checker framework, Nullaway these annotation tools in professional development work? Thanks

Ответить
@ayed-al-nator
@ayed-al-nator - 26.03.2023 01:53

Thanks ❤

Ответить
@shahrokhfiroozi
@shahrokhfiroozi - 07.01.2023 07:27

Thank you John

Ответить
@VuLinhAssassin
@VuLinhAssassin - 17.12.2022 11:54

Company will start adopting Java 17 when I am a feeble old man.

Ответить
@ksham55
@ksham55 - 22.11.2022 20:31

maybe i'm fully wrong, but why no a try catch?

Ответить
@saronabi1387
@saronabi1387 - 18.11.2022 03:45

thank you so much, i got what i want

Ответить
@m.hakania2251
@m.hakania2251 - 17.11.2022 16:34

Hello John,

Thank you for this wonderful video!!

Currently I am working on a project someone else wrote before, and I an new to Microservices (IoC & DI, all that fun);

Since it's structure is a bit complicated and not usually we can use null checker in production environment, how can we avoid that?

I am new to Microservices, and noticed recently when I wrote some codes, it came back with null pointer in the log, and I had to spend some time to find the issue, sometime more than others.

Ответить
@HouseExpertify
@HouseExpertify - 20.09.2022 14:22

You can also avoid null pointers by ensuring data basically can't be set to null.
For example, the .setName() method in his example could easily have a null check where it sets the name to a default value like "Unknown" when you call setName(null).

Ответить
@meghraj
@meghraj - 18.09.2022 17:31

thanks a lot... for sharing this.

Ответить
@romulosendoromulo
@romulosendoromulo - 16.09.2022 16:38

Hey, thanks for that

Ответить
@Swathi_World
@Swathi_World - 14.09.2022 16:38

Hi John
Why null pointer exception is occuring while I'm entering a policy number after clicking on run illustration button it's show error that is null pointer exception why this occur can you pls exam explain this plsssss

Ответить
@pyrophobia133
@pyrophobia133 - 14.09.2022 06:14

gah!

Ответить
@swapnildubey414
@swapnildubey414 - 06.06.2022 09:15

Great explanation, thanks

Ответить
@MrFieseMoep
@MrFieseMoep - 23.05.2022 08:52

Or use Kotlin. There is a button up there in the Intellij to convert your Projekt to Kotlin. PRESS IT!

Ответить
@ccruzp06
@ccruzp06 - 18.05.2022 01:13

Man, I really hope Java finds a way to add nullable types to the language. Once you start using them on Kotlin, you really miss them ):

Ответить
@Pacvalham
@Pacvalham - 16.05.2022 15:36

//Shortcut for throwing a NullPointerException:
throw null; //The downside is that you can't give it a custom message.

Ответить
@WthyrBendragon
@WthyrBendragon - 13.05.2022 22:43

I get to be the guy who introduces fresh-out new hires to our code base. There are two things I grind into them from day 1.
* Always check for null!
* Source commentary is like sex. When it's good it's VERY good. When it's bad it's usually better than nothing.

Ответить
@WthyrBendragon
@WthyrBendragon - 13.05.2022 22:41

I see a lot of situations of String comparison in which our process is case insensitive. For that we use

if ("Cosmo".equalsIgnoreCase(myOtherCat.getName()) { ... }

Ответить
@chvis002
@chvis002 - 04.05.2022 23:41

Man I work at company with a pre 14 Java version and you have no idea how many times I had a npe on a line with like 5 different objects and I would have no idea where to start. I actually didn’t know about the new exception text, very nice!

Ответить
@socaljusticewarrior558
@socaljusticewarrior558 - 27.04.2022 07:00

Another great video, John. I've gotten to the point in my practice where I'm considering initializing stuff to null. This came just in time.

Ответить
@alexei3366
@alexei3366 - 23.04.2022 11:16

But what about Optional? Why you not mention about this? I think it's good approach to avoid NPE

Ответить
@dhaw
@dhaw - 16.04.2022 08:55

Very helpfull. Thank you !

Ответить
@Abdullah-rv1pk
@Abdullah-rv1pk - 15.04.2022 03:14

Hey John please please make series on SpringBoot and microservices :(
The way you explain stuff is Amazing

Ответить
@hammett79
@hammett79 - 12.04.2022 16:14

Great video! This is java 11 or above?

Ответить
@altus3278
@altus3278 - 11.04.2022 21:44

"Null was a mistake"
- Tony Hoare, inventor of Null

Ответить
@OneShout100
@OneShout100 - 03.04.2022 23:52

Hi John! What's about Optional objects? That's a great way to get round Null values and NullPointerExceptions 8.-)

Ответить
@vincenzofortunato3461
@vincenzofortunato3461 - 03.04.2022 19:31

I think you missed an important part of "strategies to avoid null pointer exception" that is "design your code to not use nulls at all!" Instead of nulls use default values, empty collection or throw an exception. This way you can avoid all the null checks and make the code more readable. If a null pointer exception happens that is for sure a bug that needs to be resolved. A null check with an if statement is just a workaround the issue in my opinion and is a very bad practice in modern java code.

Ответить
@DKaldes
@DKaldes - 03.04.2022 14:58

I'm not even a Java developer, I use C# but I still enjoy watching your videos and for a lot of your videos I still learn from them as Java and C# are really similar.
I don't know what it is about your videos, is it your voice or the way you talk but I love you videos.

Ответить
@JeroenV1602
@JeroenV1602 - 03.04.2022 11:01

Wasn't this video also a good opportunity to talk about the nullable and not null annotations so that your IDE's linter can help find NullPointerExceptions early?

And for the String comparison at the end you could have also used Objects.equals(string1, string2) which will do a null check for you.

Ответить
@snoopy1alpha
@snoopy1alpha - 02.04.2022 23:46

Objects.equals(...) also avoids NPEs very successfully. However, the best way of avoiding NPEs in the JVM is introducing the language Kotlin in your Java project. It allows you declaring anything as not Nullable on language level.

Ответить
@caranthirn7400
@caranthirn7400 - 27.03.2022 23:41

Thank you John, great explanation.

Ответить
@TasneemAlEmam
@TasneemAlEmam - 27.03.2022 01:57

thanks man !, can you please explain the "optional" keyword it is also used to avoid null values, please add it as you make things super easy :), waiting

Ответить
@tommilton1458
@tommilton1458 - 25.03.2022 23:20

String defaultMessage = “Excellent video as usual”;

System.out.println(defaultMessage);

Ответить
@alekhgarje
@alekhgarje - 24.03.2022 13:59

Please do JVM, JDK, JRE. I have been coding since 2 years yet struggle to understand it. Please help.

Ответить
@arthurkelley
@arthurkelley - 23.03.2022 11:56

Is java dead for new projects? Is java only support 1.8 jobs or migrating 1.8 to 1.11? They say everywhere that python is new java, I understand that you make these video to sell your java course, but can I ask you to be honest and answer my questions.

Ответить
@bunnihilator
@bunnihilator - 23.03.2022 03:22

The solution for this problem is: Optional. Use on methods that may or not return a result. It will force you, on the client code, to handle it. Thus avoiding null pointer situation.

Ответить
@ody7850
@ody7850 - 22.03.2022 09:51

helps a lot for my essay.. thy john!

Ответить
@venkateshmarneni385
@venkateshmarneni385 - 22.03.2022 07:42

Hi john, could you please explain us the difference between object and instance.

Ответить
@gringodf83
@gringodf83 - 22.03.2022 01:52

Hi, Jonh. Nice video. You could talk about the garbage collector. It's a good subject.

Ответить
@zairsadiqov356
@zairsadiqov356 - 21.03.2022 20:52

Talking about nulls and not to say anything about Optionals? :)

Ответить
@gabucarneiro
@gabucarneiro - 20.03.2022 23:26

I always learn something new with you! Thanks a lot!!

Ответить
@hrishikeshgarud3077
@hrishikeshgarud3077 - 20.03.2022 16:46

You are THE BEST. precise and concise to perfection

Ответить
@Jurigag
@Jurigag - 20.03.2022 13:32

There is also 3rd option - just catch this exception in other place and never care about it. You should avoid null/nullchecks in your code as much as possible. This technic is called fail fast and i would recommend it to anyone.

But i also like the way you did it here with ArrayList - like return actual fake null implenentation which will have the same methods, for example by including interface, but i wouldn't do things like return 0 etc, sometimes when debugging your code why something is behaving the odd way - it can be actually this 0 thing.

Ответить
@my_love_sanatan
@my_love_sanatan - 20.03.2022 07:55

Good explanation, but can't we use try catch to handle NPE?

Ответить