 Hi, this is Christian. In this video, I'm going to show you some techniques how to help you debug your code in Android Studio. If you're not used to this IDE yet, you may not be familiar with some of the tools it provides. Here is the app that I created, just a simple app with the empty layout. Before you open any code, you see here, if you look at the bottom of this page here, it's kind of small. Let me see if I can zoom in for you. Let me zoom it in. Okay, so way down here at the bottom. I'll just pause it here for now. You see these tabs down here, to do, problems, terminal, build, inspector, log cat, and profile. There's one more you don't see here until you run the actual application. Let me go back to the unzoom portion and let me turn this off. Hopefully you can see it. For the to do part, as you can see, if you click on it, it's nothing here yet. It just says no to do list. This is actually really helpful if you not to use it. Maybe I can show you how to do that. The problems here is just to show you the current file you open if there's any syntax problems. Synthetic errors, it will list here to help you fix that really quick. The terminal is just like your console terminal. This is the same as going to this terminal here. The same terminal it's built into the IDE so you don't have to exit out and switch screens. The build is, as you can see here when you run and build your app. So let me just clear this for now. You can see that when you build your app and run it's going to build and then during the building process the compiler will spit out any errors if you have any here. The database inspector, for databases we'll do that later and when we try to connect to the SQLite database the log cat here as you can see already some information here. Let me clear it now. You can filter out some log data you use the log class to do that for you. The profiler here is just basically to profile the current device that you're using. It tells you like memory addresses and CPUs and things like that here. I ran this earlier so you can see some information here so usually it should be just blank. Look through here if you want to do that. On the right side you have the event log it logs all the events that happen in your current app and then the layout inspector really gives you another overview of the layout of your current layout. Again it's kind of hard to see here but if you go and run your file you'll see the layout here. Again you probably don't need this but the most important ones you'll need are the log cat and the run which you don't see here. You will in a minute once you run it you'll see it. If it's not visible you can always turn this on and off from the view menu up here. So if you go to view under the tool windows you see all these here so you can see that the build is here it's down there the run is not shown. If I click on this one here you see that it now shows down here. So if you happen to accidentally turn this off you can just say remove from slide view and then you can turn it back on again by going to the view and turn it back on. So all these are available here. Now I'm going to go and open the file in the app source and then the main Java that's a long way to enter the main file. So this is what you usually get by default anyway. Now syntax errors as you can see it will outline here for you by the IDE. We usually mark a red color text and also give you some red squiggly lines to tell you. As far as now you can see this is a perfect. No errors, nothing here. This little icon just means like you're overwriting some methods here. What you want to pay attention to is under right column over here under this green check mark you'll see this right column will point at some errors. If there's any warning messages we'll also point that out. So for example if I go up here and just type in something like high. You see that it gives you a red text. Any red text here usually it means an error. Again based on the setting, the default setting, if you change the color scheme of your theme it may be different but with the default setting this means an error. You see this squiggly line here this is the problem because it's caused by this right. So it's like a dominant effect. And then now you look at your lines you code here. You see on the left window all these have this red underline all of them all the way down to the file you have. If you have multiple files which other files have errors it will point that out for you. So you don't want any of these red things going on. If you do have those your app will not compile it will not run. On the top right you can see here we have a bubble with the exclamation mark in the number three. Now if you hover it will tell you where it is. If you click on it notice it opens the tab down here at the bottom says problems. It tells you what those three problems are. And usually if you fix the very first one it solves the problem. Notice you may not see this first because I turned this on. You may see something like that. If you double click on it it will take you to that file on that location. And you can fix that problem. And then on the left side of this you will see an icon that has a shaded columns here. If you click that and it will open the terminal here for you as well. I mean the editor here. The benefit of this is as follows. Let's say I have a real large file, large code and some way down here I'm in line like line 50 or something do some stuff down here. And just put some comments here. I'm doing stuff down here whatever it is. And I have a problem in my code. So I don't want to lose this place in my line of code. Because if you double click on this line it takes you your cursor back to line nine. Once you fix the problem where was I before. So you lost your place. Now if you were to use that. So I was here again if I use this editor over here, preview, open that and it takes you to right where the error is. And then now I can just fix the problem. If I fix it and that goes away all the problems are gone and I'm still here just doing what I need to do. So that is really useful for that purpose. So again what I showed you earlier was if I put a message here it tells you that error and if you try to run it it's not going to work. So let's say you're not aware of that and you try to run this application. What I mean is that during the build it's going to crash. So you can see that now the build tab has been activated. It tells you what it is. And also points out exactly what needs to be done or fixed. Sometimes it's not always correct. But you may not see this. Yours might look like this. You have to scroll all the way to the right to see everything here. So I recommend you turn this little soft wrap on so that you don't have to scroll all the way. You can see your line of code and the errors and it will tell you the error is it says illegal start of type and overwrite. It doesn't tell you much. It's like it thinks that this is a data type. If you remember how to code you do something like I, A like that. So this one here doesn't matter where you put it. You can put it down here. It doesn't matter. As long as you put a semicolon then it will terminate the statement. If you don't do it, it thinks that it still continues on all the way here. So you have an error. So that's what it tells you. It's a type. This is a type. It thinks that the high is a type and this is a variable of that type. That's what it's trying to tell you right here. And then on the left side, so before the build fails, you watch out for the build information tells you here. Let's go back to the problems again the same as before. If I tell you it's all that, if you look at the little light bulb here it shows a quick fix it suggests that you should fix this. So if you click on it, it says create the type parameter rather high. If you accept this, it's trying to fix it for you. And what did you see? Now it caused some other problems but it fixed this. Now it changed also your class name, main activity, and with the angle bracket and the word high in there. This is known as generics. If you're not familiar with generics, you may not understand what this is, but it's trying to say that this is a data type that doesn't exist. You can make this a generic type. Now usually in type, you follow the letter t and you use that in your code, like t of some is something like that. It's a generic type. If you remember using array list, when you create an array list, you always use the angle bracket to pass in a data type. So the array list stores numbers strings or whatever it is. It's trying to fix it for you. So that's just one really common fix this in here. Okay, so we saw that, that's the error. And then let's say that if I go in here again, if I mistype the letter s here, I use the capital letter and then again tells you in red. Anything you see in red here again, it's an error. Syntax error usually. Or something that's not available in classes not being imported. You've done that before if I were to do a text view and then a, you see that this one imports automatically, which is great. But something that's not important automatically like maybe view, view. You see that it's not imported, so I have to do that import. You know that already. It's the same idea here. Similar thing. So I did that and it tells you again on the right side is a red error. It says cannot resolve the method set content view. It gives you a suggestion. Should you create a function for that? If you click this, it will create a function for you. Using this as your function, which may or may not be what you want. But in this case, we don't want that, right? So again if you see this if you're not sure if you're trying to run this, it will also you know, capture that in the build. So again the build called that it's illegal. You cannot find this symbol, meaning this function or whatever it is, it's not found, doesn't exist. So you have to fix it. Well in this case it may be difficult if you don't know the rules, all the classes and functions in here. So I mean once you know that you know but if you don't know then it could be a little bit tough. But anyway, so we know that this is not allowed is not part of the function. So we'll fix that. And then say I also use a different activity I call activity 2. It doesn't exist. Again red. So that's one thing it can tell you. Now the next thing that usually happens is, so you see syntax errors are easy to fix. The more difficult ones are the logic and runtime errors. So let's say I have a integer total is equal to 1 divided by 0. We know that division with 0 is not allowed in Java. In JavaScript other languages may be okay. They will actually set a type to infinity but in Java we know it's not allowed. So therefore of course the ID doesn't know that until you run it. So this is not a syntax error. It's a runtime error when you run. So for example if you never run this line of code in your program it's no problem. Until you reach the syntax this line it will crash. So let's create my screen over here. Close my console and I go to the run. Again just make sure nothing happens. Okay so now I'm going to build and run this app. So you can see you saw very quickly it loads and then it tries to load it and then it turns it off. Just to make sure it does work let me comment this out again. Okay let me turn this emulator on. Okay so let me just lock this in. I can lock it in. So here we go again if I run it one more time. You'll see that it tries to load. So now it's okay because now in the app it's running. Right no problems. Now again if I turn this back on if I save and run it again you'll see that it tries to load and then there's a problem as your app closes. You may see this message or you don't see at all. The little white flashes and then it goes away. That means there's a problem in your app. It doesn't tell you here there's no red lines, no nothing to tell you so you can't know what that is. So how to solve this problem is by checking in the run tab down here. Okay the run tab tells you pretty much everything when the app is running. So let me maximize this a little bit more. It has a lot of information down here. Everything that runs in the app every time you click something it loads the activity it does something you know it generates all this information here. So you won't look for anything that's in red usually in red again. So if you scroll up and down you will see that at the very bottom you see a bunch of red text. Okay and that's usually problems. Then if you scroll up from this line because one problem will crash your app right away. And if you scroll up it tells you right where this blue line is it says in the main Java file line 15 that's what this means. If I click on it it takes you right to that line. And the error is usually right above it says division by zero. Okay it's caused by division by zero it's not allowed. And if you keep scroll up it's going to repeat again but I will tell you until like all this blue and co-text those are okay. It tells you here that the Android VM virtual machine was shut down here. It's a fatal exception, a fatal error in the main program. And it trades down here to show you where the error is. The error is caused by again it's unable to start activity because of this exception. So this is hard to find but if you scroll down until you find this blue text it tells you exactly where it is in your code. And the error is right here. So you need to fix that. Okay so if you see that your app is running fine and there's no problem but you don't understand why it's not running, check in the run. And because it's really hard to see this is the run is like the console and other IDEs like you run Eclipse, open the console or visual studio code in the console that's what you see here. Okay so if you write out statements you also shown up here. So let me show something here. For example if I put a correct number let's say you know 100 divided by 10. So I'm going to get 10. And if I want to print out the total so I can see in the console you will call this function called system.out.print. And then you put here some text total and then you put here concatenation operator and then total here. So it will print total is 10 in this case to the console. So I fix this error so if it won't crash it will load the app and then now let me clear my console down here. Clear all, right click and then run this again. Okay so it loads the app, everything looks good. It goes and create this app here it runs everything in the background and then it should stop at some point. Okay so now you can see that you know everything looks good. No red things in here right. If I scroll up there's no red things. Okay but there's a lot of things going on here and I'm sorry if I scroll too fast. If you go up here you should see go to the top. Okay so I couldn't see the the black text. You should see the black text in here where is it at? I know it's in here somewhere it's a message that we print out. It should be at the bottom. Yeah that's a lot to find so let's go down here again. Well it's I know it's there it's kind of hard to find which is why you want to use the tag. So if I go here again I'm just going to turn that off and then open it again okay and well I don't see it for whatever reason it's not shown here but your system I should print a message here. Let me try to clear that again and then I'm going to just rerun the app. Okay so we go again. Yeah that's I think that's after the fact let's go all the way to the top again. Okay any message out here. It launched successfully yeah I'm not seeing the message for whatever reason it should be here which is why it's so hard to find as you can see you could you'll find it should be out there I'm not seeing it. So that's why we talked about let me close this the log okay you can see it's hard to do that. So the log if you remember log.d and you pass in here a flag filter variable you can look at. So for example here just data maybe okay and you pass this string is I want to print the total I want to print this message here so you can put here like this okay and then the log need to be imported so let me import that. Okay so now I can track this data easier in the run. Okay so again if I run this variable let's rerun this again. Yes okay so if I go to the log cat you will see that in the log cat it's much nicer again has a similar stuff it's really hard to find that. It should be in here somewhere it's hard to find. So that's why I use the data as a filter I can add in the search box here for data I only look for that data okay and that's other data out here too. I should make it more unique but let's see let's try and run this again one more time. Okay so you see now that it filtered out all the things I don't want. Again right click clear. Okay do one more time run okay there you go you see that now this is what I have right I filter everything out I just want to see that this is working correctly my calculation is working correctly so it shows up here and that's what it's for. So you can create a log of other stuff log data log events users information whatever it is you filter it here it will give you that output here. So instead of going to this it's hard to find use the log data to do that for you okay so that's for that purpose so the next thing I want to show you is you can actually set points in your target and let me go back to this again let me close this for now so let's say you can trace your code okay you can trace line by line and see what happens so if you have a lot of code and you don't know what's going on instead of hand tracing it's really hard to do you can you know set a marker here so on the left column here right here if you click on it it gives this little red circle here it tells you that when the application runs when the you know reaches this line it will pause execution at this point from here on you can step through line by line of code you can see what's going on okay so if you aren't sure you can do this and then once you find the culprit of the error you can stop there so let's say I'm going to pause right here when I reach this line and so when you put this here to make this work you have to click on this debug icon here the little bug so if I go and click that now yeah I'm going to restart everything from app again okay it's waiting for that to bug and so here it is you'll notice that when it runs it pauses right here at line 14 exactly where they put the marker on and down here if you look it loads that debugger frame and it tells you at line 14 you're here and it also tells you some variables here this refers to you know the current object in this case it will be this activity object right and then the same instance is no so it knows that because we already executed this part right so this one has no data saved into the instance so nothing there now if I scroll down and look at these icons here one of these is a step over step into you know four step into the step into means if you're calling a function then it's going to go into the function and it keeps going down until you reach the end of the function you come back up so that may it may not be what you want so if you step over then it just means run this statement and then it doesn't matter how many functions down there just skip those and come back up okay so I'm going to click this step over and then now I'm going to the next line so at this point you know at the view I forgot to see here but you can see that the view has been created here I'm now running a line 16 okay if I go again it tells you that my total has now been created you can see it's visible now in the variable and it has a stored value of 10 already okay so next thing is going to print this out to the console that's been done and then next is log out to the cat okay so if I go to the cat you see that right now there's nothing here yet right so let me clear the console for now there's no data here yet if I step through it executes the line it should be added to the log if I go back to the log you see that it's now added here okay so you can step through you know your code line by line this way and again if you are done then now you're back to this because it loads the activity now and it keeps going until whenever you stop okay so you can just you know stop this and you're done so that's a tool to help you debug your code line by line okay put a marker here it will do that for you okay well there are many more you know errors that can tell you one more thing I want to show you a common error that you might have is the following so let's turn this off let's go and create a second activity I'm just going to copy here okay go to the res layout and I'm going to copy here just ctrl c ctrl v and I created activity main two okay so now this main two here looks exactly the same as the one but I'm going to go and create some variables in here so let me go in then I'm going to use the sex view given id id of txt you know input or maybe just put hello okay that's the id I give to this second view the first one doesn't have it okay the first one here has no id as you can see it should not have it which is fine the second has this id now in the code over here if I want to access this view so I'm in the main activity and so if I go here and if I try to get the data from that view so I go text view right and tv here is equal to you know text view let me cast that to find by id or id dot you see that text hello is here even though I'm using the main activity okay so this tells you that it doesn't matter which activity which layout I use I can have 10 layouts or 20 doesn't matter if I have one unique id called text id all those ids will be visible here and if I try to access this thinking that this is from the main activity layout as you can see there's no problem here if I try to access this and run it right you will see that it might generate an error so again go and run this app now and we'll see what happens okay so that's fine because it will be able to access that so now I want to do something with the text view I'm going to overwrite this text view with a new text so usually you can use the set text tv.setTex change the text to high okay so let's save this again and rerun and now again back to the problem we did earlier nothing runs no error in your code no red lines anywhere at all just have a warning here but that's nothing critical so you can see that again back to the error how do you know this you cannot find it and here you go to the run look for all those red lines again it will tell you usually in the bottom again tells you down here it says line 21 if I click on it it tells you line here it says there is no object reference okay you're trying to call the text view.setTex but it's no no means it doesn't find this object so therefore you have to kind of be careful with this because you know idea doesn't tell you much more than it tells you here but you know that this exists but it says it's no it's not there well because you're running the main activity when you run this in the running mode it connects to the main activity file which is going to look in here and there is no ID matching that okay so because you pull that from here instead so that's something you want to watch out for when you run your code or so okay I think that's enough for now I know it's a long video but I hope you'll find this useful if you have any questions if you have other things you want to review please let me know thanks