Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Errors in Java can be separated into two major types, compile time and runtime errors. A compile time error is either a syntax error or an error in the structure of the application. Syntax errors are pretty straightforward. Let's say for example that I declare a variable named s and I give it a value, but I don't put the semicolon at the end of the line. That breaks a fundamental rule of Java syntax. All statements must have a semicolon. And when I save and try to build the application, I get this error down at the bottom in the problems view, Syntax error, insert semicolon to complete BlockStatements.
Other kinds of compile time errors can occur when you break fundamental workflow rules. For example let's say I declare a variable named s, I put in the semicolon at the end of the line and that line on its own is syntactically correct. But then I immediately try to output the value of s using System.out.println and I parse in the value. And so I have two syntactically correct statements, but when I save the application once again, I get an error in my problems view.
If your errors are represented by this red circle with x, that means it's an error that is preventing the application from compiling. And if you try to run the application you'll get this error dialog. Errors exist, Proceed with launch. If you proceed at this point, your application might seem to run, but you'd be running an old version of the application, not the one for the code you have on the screen right now. If you have errors like this your application can't be compiled.
In this case the rule that you are breaking is that you can't declare a variable without initialization code and then refer to that variable immediately. The most common fix to this is to add equals null and that would make it so that the compiler decided you knew what you were doing. Because I declared the variable and gave it an initial value, I can now compile and run. And when I run the application I just get the word null. So that's a compile time error. The other type of error is a runtime error.
These are errors that occur because you break some rules that can't be caught by the compiler but when you run the application the error is so bad it just kills the application, it crashes it. Here is a very simple error that occurs a lot. I'm going to declare a new string array variable, I'll just name it strings. And I'll give it an initial value of one item, "Welcome!" This is one way of declaring an array in Java by wrapping comma delimited list inside a pair of braces.
If I only declare one value inside the braces, then it's an array of one item. Now I'm going to try to output an item in the array using System.out.println and I'll refer to strings, open bracket, 1, closed bracket. This is clearly an error. Arrays in Java are zero-based. If I want to refer to the first item in the array, I need to refer to the index zero, not one. If I'm referring to one that means the second item in the array and it doesn't exist.
When I save my changes, the compiler says looks good to me and I don't see any problems in the problems view. But when I run the application, I get a big odd error and my application just stops working. Exceptions in Java cause the application to quit unless you've handled them yourself. The particular error that's occurring is something called ArrayIndexOutOfBoundsException. In the next video I'll talk a little bit more about exception classes or exception objects, the kind of objects that are generated in Java when an exception occurs and how you can look at the exception object both in the Java debugger and in your own code.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100129 Viewers
56 Video lessons · 113153 Viewers
71 Video lessons · 82024 Viewers
131 Video lessons · 39354 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.