java.lang.NullPointerException Example – How to handle Java Null Pointer Exception

preview_player
Показать описание

JCG Related Articles:

Sections:

00:00 - What null is
03:30 - Nullpointerexception example
04:45 - Ways to avoid/handle Nullpointerexception
05:00 - Check with if
07:20 - Ternary operator
11:20 - Throwing exception on null
12:45 - Optionals
15:50 - Nullpointerexception with Maps
19:10 - Assertion
21:00 - InstanceOf

Instructor Mike Møller Nielsen:
Mike was born in 1977 in Kolding / Denmark and he has a passion for programming and tech in general. He works on freelance basis mostly in Denmark where he develops full stack applications mostly written in Java with Spring Boot. He has a bachelor in Chemistry which he took before migrating to IT engineering. He has a YouTube channel where he spreads the knowledge and solutions to every day problems.

Follow Us:

#Java #JavaTutorial #JavaProgramming #LearnJava #Java.Lang.NullpointerException
Рекомендации по теме
Комментарии
Автор

I was surprised by the content then I went back and re-read the title. I found the information informative and well presented so thank you for taking the time to produce this.

johnmcneil
Автор

This video is amazing. You covered all the good examples of handling java null pointer exception. Thanks so much!!

julikim
Автор

Please help I use samsung a7 mobile I have same problem help me

hardeepdhillon
Автор

Can someone help me to understand why is this getting null.pointer.exeption?

Ship Ship;
//PImage ship;
float x = 100;
float y = 100;
float vel = 1;
float aceleramento = 0.7;
float acelerar = 0.7;
int vel_maxima = 30;

void setup() {
size(1000, 1000);
//ship = loadImage("ship.png");
}

void draw() {
y += vel;
vel += acelerar;

background(255);
Ship.rotating();
Ship.actions();
}

class Ship {

void rotating() {
pushMatrix();
translate(x, y);
circle(x, y, 20);
rotate(degrees(0.0005 * vel));
rect(x, y, 150, 50);
popMatrix();
}

void actions() {
if (mousePressed) {
if (mouseButton == LEFT) {
acelerar = -aceleramento;
}
} else {
acelerar = aceleramento;
}

if (y > height - 87) {
y = height - 87;
vel = 0;
}

if (y < height - height) {
y = height - height;
vel = 0;
}

if(vel > vel_maxima) {
vel = vel_maxima;
}else if(vel < -vel_maxima) {
vel = -vel_maxima;
}

if (dist(height - height, height - height, y, y) < 30 && mousePressed) {
y = height - height;
vel = 0;
} else if (dist(height - 87, height - 87, y, y) < 30 && !mousePressed) {
y = height -87;
vel = 0;
}
}
}

Thanks!

vinijoncrafts
Автор

Some use ternary operator to check for null and if null it returns a default value.

mr.RAND
Автор

(nullableString, defaultString) ->

NBRIUM
Автор

Thank you! P.S. IMHO Your intro too loud.

MagDag_
Автор

Why you didn't use a Try, catch? I'm beginner in QA automation

MagDag_
Автор

Thank you for your try. Null errors are fake news no doubt. I listened to 5 min. of your video and if I didn't know what nulls were, I sure the hell don't now. Null pointer exceptions are the result of sloppy code. But I understand that we all make mistakes and miss something now and then. But fuck, this is not brain surgery.

Nusremmus