Programming - 2. Common Errors

Similar documents
For more details on SUN Certifications, visit

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java: Comment Text. Introduction. Concepts

Std 12 Lesson-10 Exception Handling in Java ( 1

Fundamentals of Object Oriented Programming

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS159. Nathan Sprague

Practice Questions for Chapter 9

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Chapter 1: Introduction to Computers, Programs, and Java

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

CSC Java Programming, Fall Java Data Types and Control Constructs


Exception Handling CSCI 201 Principles of Software Development

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

BBM 102 Introduction to Programming II Spring Exceptions

Data Structures. 02 Exception Handling

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Options for User Input

C16b: Exception Handling

Java Errors and Exceptions. Because Murphy s Law never fails

DD2460 Software Safety and Security: Part III Exercises session 2: Type + Jif

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

More on Exception Handling

Exception-Handling Overview

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Exceptions Handling Errors using Exceptions

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Full file at Chapter 2 - Inheritance and Exception Handling

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Class 9: Static Methods and Data Members

BBM 102 Introduction to Programming II Spring 2017

S8352: Java From the Very Beginning Part I - Exercises

Warm up question. 2)Which of the following operators are defined on a half integer? Which of the following would it be useful to write as behaviors?

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Correctness and Robustness

Selected Questions from by Nageshwara Rao

Programming for Mobile Computing

Oracle 1z Java Standard Edition 5 Programmer Certified Professional Upgrade Exam. Practice Test. Version:

PASS4TEST IT 인증시험덤프전문사이트

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Recitation 3. 2D Arrays, Exceptions

More Control Structures

JAVA GUI PROGRAMMING REVISION TOUR III

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

1B1b Classes in Java Part I

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Supplementary Test 1

Binary search. int index = Collections.binarySearch(list, element);

Programming overview

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

*Java has included a feature that simplifies the creation of

More on Exception Handling

Select the one correct answer.

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

1. Download the JDK 6, from

Errors and Exceptions

Here is a hierarchy of classes to deal with Input and Output streams.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

this keyword in java javatpoint

JAVA Programming Language Homework I - OO concept

Introduction to Java. Handout-3a. cs402 - Spring

COMP1008 Exceptions. Runtime Error

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

Section 2.2 Your First Program in Java: Printing a Line of Text

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

CSCI 261 Computer Science II

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Class, Variable, Constructor, Object, Method Questions

Input from Files. Buffered Reader

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Oracle Exam 1z0-809 Java SE 8 Programmer II Version: 6.0 [ Total Questions: 128 ]

1 Inheritance (8 minutes, 9 points)

Chapter 5. Feb 08, Chapter 5 1

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

this keyword (1). this with constructor (2). cisc3120 design and implementation of software applications I spring 2015 lecture # I.

Improving Exception Messages with ExceptionDoctor

SUN Sun Certified Associate for the Java Platform.

One of Mike s early tests cases involved the following code, which produced the error message about something being really wrong:

Tutorial 8 Date: 15/04/2014

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Nested Loops. A loop can be nested inside another loop.

Transcription:

Common Errors There are certain common errors and exceptions which beginners come across and find them very annoying. Here we will discuss these and give a little explanation of what s going wrong and how you can avoid that. Given below is a list of common mistakes along with their explanation. NoClassDefFoundError NoSuchMethodError Cannot resolve symbol Local variables must be initialized ArrayIndexOutofBounds & NumberFormat exception NullPointerException Unreachable Code Umair 2006, All Rights Reserved - 1 -

NoClassDefFoundError This error occurs when the class you are trying to run a class which does not exists there. It could be due to wrong name that you are specifying or may be you are in the wrong directory. (e.g. you have saved the file in ex1 directory and trying to run it from ex2 directory) In the example below the name of the class is CommonErrors. The user has compiled the class correctly but running it using a wrong name CommonError. System.out.println( Hello World ); Umair 2006, All Rights Reserved - 2 -

NoSuchMethodError:main You are trying to run a class which does not have the main method. main() is where your code starts to run and if a class does not have the main method and you try to run it you will get this error. The following code does not contain the main method and when the user is trying to run that code, he/she will get the error that we have specified above public static void display () { System.out.println( hello world ); Umair 2006, All Rights Reserved - 3 -

Cannot resolve Symbol You are trying to use a variable or method does not exist. It can be due to incorrect name, wrong number or order of arguments etc. In the example below we have declared variable with the name number but we are trying to use the variable with the name number1 int number; System.out.println(number1); Umair 2006, All Rights Reserved - 4 -

Variable might not have been initialized In java variables declared at class level, get initialized automatically but the variables inside a method does not get initialized automatically. Now if we try to use any such variable as a right value (if we try to assign its value to some other variable) or in a method, we will get this error. In the example given below variable number is declared inside method main and is not initialized and you can see that when we are trying to print its value we are getting this error. int number ; System.out.println(number); Umair 2006, All Rights Reserved - 5 -

ArrayIndexOutOfBoundsException : Arrays are bound checked in java. It means that you can not access an index of an array which doest not exists. If you try to do that this exception will be thrown In the example below we are trying to access the first index of the args[] array but while running the program we have not provided that index in the first run and hence it caused an exception but in the second run when we have provided a value exception did not occur System.out.println(args[0]); Umair 2006, All Rights Reserved - 6 -

NumberFormatExceptions Your are trying to convert a string or an object into a number, which is not actually a number, NumberFormatException is thrown In the example below, we are trying to convert a string 20s into a number. As you can figure out that this is not a number and hence exception will occur. String s = 20s ; int number = Integer.parseInt(s); Umair 2006, All Rights Reserved - 7 -

NullPointerException : You are trying to use a reference which is not pointing to any object. This is a very common problem with the beginners and usually occurs due to the failure of calling constructor to initialize an object. In the example below a variable s of type Student is declared, but its constructor is not called and hence s is not pointing to any object. Now if we call a method on s then NullPointerException will be thrown because is pointing to null. public static void main (String args[ ]) { Student s = null; s.print( ); Umair 2006, All Rights Reserved - 8 -