CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
COMS W1004 Introduction to Computer Science May 30, 2008.
Your First Java Program: HelloWorld.java
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
How to Create a Java program CS115 Fall George Koutsogiannakis.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
CS115 HOW TO INSTALL THE JAVA DEVELOPMENT KIT (JDK)
COMS W1004 Introduction to Computer Science June 3, 2009.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
CS 201 Functions Debzani Deb.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Games and Simulations O-O Programming in Java The Walker School
Hello AP Computer Science!. What are some of the things that you have used computers for?
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CPS120 Introduction to Computer Science Iteration (Looping)
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Introduction to Exception Handling and Defensive Programming.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CPS120: Introduction to Computer Science Decision Making in Programs.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Programming in Java CSCI-2220 Object Oriented Programming.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
When I want to work with SQL, I start off as if I am doing a regular query.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CPS120 Introduction to Computer Science Iteration (Looping)
Java Programming Fifth Edition Chapter 5 Making Decisions.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh.
Christina Aiello, 12/3/2015. Reminder: Seen in lecture notes for Exceptions: “Side note: we will NOT expect you to know/remember how to do keyboard input/output.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Error Handling Tonga Institute of Higher Education.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Introduction to Exceptions in Java CS201, SW Development Methods.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Computer Programming Your First Java Program: HelloWorld.java.
Introduction to Java Import Scanner class to use in our program
Testing and Debugging.
Introduction to C# Applications
Cracking the Coding Interview
Packages and Interfaces
Tonga Institute of Higher Education
An Introduction to Debugging
Exceptions 10-May-19.
Presentation transcript:

CS116 COMPILER ERRORS George Koutsogiannakis 1

How to work with compiler Errors The Compiler provide error messages to help you debug your code. The error message will give you the line number where the code fails the compiler. Sometimes the code in the line number given is not the real cause of the problem and we have to search further. Start by working the very first message at the top. Sometimes correcting that can take care of a number of compiler errors. 2

Examples of messages and what to look for 3

In the previous slide there are 3 errors. The reason the library class Scanner is not recognized by the compiler is because the import statement for the class was missing i.e import java. Util. Scanner Correcting this will take care of two of the 3 errors. The third error is a result of not having declared the variable register. Using a variable without declaring it will cause the compiler to indicate that it can’t find the symbol. 4

Examples of messages and what to look for NoClassDefFoundError You get this error when you're trying to run your code. So first ask yourself, did you attempt to compile the code? If so, did you see any error messages when you compiled? If you saw error messages, look for things you can fix in your.java file. Try to fix these things, and then compile the.java file again. In other words maybe your class was not compiled successfully. Another possibility is that you are in the wrong directory with your command to interpret. In other words your.class file is somewhere else in your directory structure. Make sure you have an appropriately named.class file in your working directory. For instance, if you're trying to run a program named MyGreatProg, look for a file named MyGreatProg.class in your working directory. Check your classpath to make sure that it contains the.class file that you need. For example, if all your Java code is in your working directory, make sure that the classpath includes a dot. 5

Examples of messages and what to look for NoSuchMethodError When you encounter this error message, check for the misspelling or inconsistent capitalization of a method name. Check the capitalization of main (not Main). When you issue the java command (or do whatever you normally do to run a program in your environment), does the class that you're trying to run contain its own main method? If not, then find the class with the main method and run that class instead. 6

Examples of messages and what to look for Cannot Resolve Symbol If you get an error message that includes cannot resolve symbol, check the spelling and capitalization of all identifiers and keywords. Then check again. If the unresolved symbol is a variable, make sure that this variable's declaration is in the right place. For instance, if the variable is declared in a for loop's initialization, are you trying to use that variable outside the for loop? If the variable is declared inside a block (a pair of curly braces), are you trying to use that variable outside of the block? Finally, look for errors in the variable's declaration. If the compiler finds errors in a variable's declaration, then the compiler can't resolve that variable name in the remainder of the code. 7

Examples of messages and what to look for Expected ';' (Or Expected Something Else) When you see an error message that says ';' expected, go through your code and make sure that each statement and each declaration ends with a semicolon. If so, then maybe the compiler's guess about a missing semicolon is incorrect. Fixing another (seemingly unrelated) error and recompiling your code may get rid of a bogus ';' expected message. For a missing parenthesis, check the conditions of if statements and loops. Make sure each condition is enclosed in parentheses. Also, make sure that a parameter list (enclosed in parentheses) follows the name of each method. For an expected message, check your assignment statements. Make sure that each assignment statement is inside a method. (Remember, a declaration with an initialization can be outside of a method, but each plain old assignment statement must be inside a method.) For the 'class' or 'interface' expected message, make sure you've spelled the word class correctly. If your code has an import declaration, check the spelling and capitalization of the word import. 8

Examples of messages and what to look for Missing Method Body or Declare Abstract You get a missing method body or declare abstract message when the compiler sees a method header, but the compiler can't find the method's body. Look at the end of the method's header. If you ended the header with a semicolon, then try removing the semicolon. If the header doesn't end with a semicolon, then check the code immediately following the header. The code immediately following the header should start with an open curly brace (the beginning of a method body). If some code comes between the header and the body's open curly brace, consider moving that code somewhere else. 9

Examples of messages and what to look for An 'else' without an 'if' Compare the number of if clauses with the number of else clauses. An if clause doesn't need to have an else clause, but each else clause must belong to an if clause. Remember, you enclose an if condition in parentheses, but you don't put a semicolon after the condition. Did you mistakenly end an if condition with a semicolon? Look at all the lines between an if and its else. When you find more than one statement between an if and its else, look for curly braces. If the statements between the if and its else aren't surrounded by curly braces, you may have found the culprit. 10

Examples of messages and what to look for Non-static Variable Cannot Be Referenced from a Static Context Lots of things can give you a non-static variable cannot be referenced from a static context error message. But for beginning programmers, the most common cause is having a variable that's declared outside of the main method. It's no sin to declare such a variable, but because the main method is always static, you need some special help to make the main method refer to a variable that's declared outside the main method. The quickest solution is to put the word static in front of the variable's declaration. But first, ask yourself why this variable's declaration isn't inside the main method. If there's no good reason, then move the variable's declaration so that it's inside the main method. 11

Examples of messages and what to look for FileNotFoundException (The System Cannot Find the File Specified) or EOFException If you encounter a FileNotFoundException message, check that the file named in your code actually exists. (Look for the file using your system's explorer or using the command prompt window.) Double-check the spelling in your code against the name of the file on your hard drive. If you've found a correctly named file on your hard drive, make sure that the file is in the correct directory. (For a program running in your working directory, a typical data file is in the working directory also.) 12

Examples of messages and what to look for There are many other compiler errors. – The java tutorial provides a listing of some of the errors. Some error messages appear after the file is successfully compiled. Those are called runtime errors. 13

Examples of Runtime Error messages A good paper on this topic can be found at the site: et/errors.pdf This is a pdf file that you can download and use as reference 14