Null Pointer Exception Java Fix

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're anything like me I went to school and they taught me nothing about null pointer exceptions I had to figure it out on my own and I had to go through hours and hours and late nights and it was awful I couldn't figure out what's going on if you're going through the same thing I'm here to help you okay I feel you I feel I feel the pain so if you watch this video all the way through you will hopefully get to fix your null pointer exception and also get to learn a little bit about what they are and how they can help you get working code hey what's up I'm Alex welcome back to my channel if you're new here I make Java tutorials just like this every single week so if you're new here then consider hitting that subscribe button so let's first start off by looking at some nullpointerexception Java code let's make our own Java file and go from there so go to file new Java project null pointer exception is what we'll call our project hit finish on the source folder right click and go to new class we'll call it no pointer again take this first check mark and hit finish let's create our own null pointer exception and then try to fix it together so an easy way to do this is just to make a string actually it's really easy to make a null point exception in a bunch of different ways that's why they're so common but I'm just gonna make a string called a and set it equal to monkey and if we'd print out a we should get monkey printed out so I'll just save it and run it and yes we get the monkey but if this was something called null that's a key word no just like int like how we type aa int here it turns purple that means it's a key word so this n ull is a key word to Java and I'm gonna explain everything here in a second but I'm just gonna save it now a is null what happens when we print it let's click the Run button see what happens we get no okay that's interesting and out itself but we're trying to make a null pointer exception so let's try to do something with our string let's say a dot to bring out what that string can do and we'll try to say like get the length of it type length and parentheses to try to get the length of that string so we'll save that and run it and we get some red words and it looks really bad because it's like it's not printing out what I want it to it's not working the first word you see here is exception I'm going to bring up the definition of exception it says an exception is an event which occurs during the execution of a program that disrupts the normal flow of the program's instructions so when we hit the Run button and execute a program job is trying to check all the code to make sure it's okay that the code checks out we're not doing anything weird but if something's not right it puts an exception on the screen an exception is kind of like an error but it helps to tell you what's going on so back to our program when we try to see the length of a we get the words exception in thread main java.lang null pointer exception and this is that dreaded a null pointer exception that is so common across everywhere the last line here says add null pointer main null pointer Java 7 this means our file nor pointer and the main method null pointer dot main is at line 7 line 7 right here corresponds to where the null pointers exception was found so it's helping us this is actually like it looks scary but it's actually a friendly message saying hey I noticed something was wrong try checking line 7 and this was the problem if you read it like that your life so much easier every time I got this I was like oh man and then I copied and pasted the exception into Google and tried to find out a way to fix it but it never got fixed so I'm gonna hopefully try to help you out now I'll start by explaining what null is because I didn't do that yet since I'm a millennial I grew up with the Internet I'm just gonna search the definition of null in Java because it probably does a little bit better of explaining than I can it says in Java programming null can be assigned to any variable of a reference type to indicate that the variable does not refer to any object or array and that's not helpful at all I'm sorry even brought that up and what it's saying is that this keyword null it's a keyword since it's in purple can be assigned to something like a string or an ArrayList or a map or anything as sort of like a placeholder a exists but there's nothing in it and that's what no means it's kind of like an empty container we have a container with I in it like a little plastic box that that makes a little sound when you hit the table our empty clear plastic container is a but there's nothing inside of it if we put the word dog in here then dog would be in the container if we put the word cat in here then cat would be in the container but if it's null that that means the container is empty if this was two double quotes like that then the container would have an empty string inside it which is different than just nothing so this makes total sense why are we trying to get the length of nothing if the word dog was in our little plastic clear container we could get the length of that it's three if there is an empty string in here we could get the length of that even it would be zero but if there's literally if there's nothing in it we can't do anything to it we are pointing this code to try to do something to nothing it's a null pointer exception we're trying to do something to nothing so when we try to run our code we can't it's an event that changes the flow of the program to not get run let's try to do some more stuff here with null in Java let's try to make an integer I and see what happens if we set that equal to null we actually can't and let's let's hover over it let's see type mismatch cannot convert from null to int an integer is something called a primitive type just like double or long or float these are all built into Java slot float these are all built into Java which means they're primitive they were there they're there before but things like string like this or a scanner like this I'm just gonna make a scanner real quick and I'll just hover over it and import that into a program and I'll just type this something in here to fix that things like string and scanner we're sort of built on top of Java so they're not primitive and so that's kind of the rule null cannot be assigned to a primitive variable but they can be to objects or things built on top of gel like this we don't have any errors here but we have an error here assigning it to an integer so let's continue like with scanner here for example a scanner gets input from user and to do that you would type the name of your scanner dot next we say next integer like next int that gets input from this console window if we save this and run it we see the red and blue text again something's not right and the first word is exception an event happen that changed what it's actually what we actually want it to do in thread main main method Java dot Lang nullpointerexception at the null pointer class which is our class dot main right there and then line eight so it says it's exactly where the null pointer exception is it's very helpful so to try to fix our null pointer exception we come here and we start on the Left we see scan dot next int that's the correct code but the action next int we're trying to do on our scan object is pointing to nothing an empty plastic container if our empty plastic container had a scanner object like this and we type system dot in in here which is just getting input from this console window if we save it and run that if we ask the user to enter number enter any number and then we print out that number back save it and run it it's your number we enter 3 we get 3 back and it's working how it should since now our empty plastic container has a scanner which helps us get input from a user it's setup right I can actually take it and use it to get an integer but if there's no scanner in there at all and I try to do something with it I can't because nothing's there it's like saying I have a pen I take the cap off the pen if there's no pen here to begin with I can't I can't do anything I can't take a cap off it because it's not there to begin with I'm pointing to nothing and that makes it not work a null pointer exception so I really hope this was helpful for you this is sort of my first time explaining it out loud and to help fix your null pointer exception just look at the object you're trying to do actions too like if you're trying to do the add method on an ArrayList make sure that that ArrayList is set up correctly meaning it's declared correctly you have the right setup for it like you have objects in there whatever you need to set it up so it's not empty so you can actually do stuff with it so I hope this nullpointerexception Java code helped you out if it did please leave a comment for me tell me what I can improve on if I can change examples or anything to help you learn how to code as always I appreciate you being here you could be anywhere in the world but you're here with me I appreciate that so much and I'll catch you guys next week [Music]
Info
Channel: Alex Lee
Views: 66,966
Rating: 4.8990183 out of 5
Keywords: null pointer exception java, java.lang.nullpointerexception, null pointer exception, null pointer exception java fix, null pointer exception in java, how to fix java.lang.nullpointerexception, nullpointerexception, null pointer, java.lang.nullpointerexception fix, java.lang.nullpointerexception error fix, java, java null pointer exception how to fix, exception handling, exception handling in java
Id: EIDOQS6x0OQ
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Thu Mar 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.