Difference between revisions of "Java/C2/Errors-and-Debugging-in-Eclipse/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title:''' Finding Errors and Debugging using Eclipse '''Author: '''TalentSprint '''Keywords: '''Video tutorial, Errors, debugging, eclipse, java {| style="border-spacing:0…')
 
Line 20: Line 20:
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you are going to  learn  
  
about the possible errors in a typical '''Java '''Program,  
+
xwhat are the possible error about the possible errors in a typical '''Java '''Program,  
  
 
how to identify them and
 
how to identify them and
Line 47: Line 47:
  
  
If not, for relevant tutorial please visit our website which is as shown [http://spoken-tuitorial.org/ ][http://spoken-tuitorial.org/ http][http://spoken-tuitorial.org/ ://][http://spoken-tuitorial.org/ spoken][http://spoken-tuitorial.org/ -][http://spoken-tuitorial.org/ tuitorial][http://spoken-tuitorial.org/ .][http://spoken-tuitorial.org/ org]
+
If not, for relevant tutorial please visit our website   as shown [http://spoken-tuitorial.org/ ][http://spoken-tuitorial.org/ http][http://spoken-tuitorial.org/ ://][http://spoken-tuitorial.org/ spoken][http://spoken-tuitorial.org/ -][http://spoken-tuitorial.org/ tuitorial][http://spoken-tuitorial.org/ .][http://spoken-tuitorial.org/ org]
  
 
|-
 
|-
Line 59: Line 59:
  
  
| style="border:0.035cm solid #000000;padding:0.097cm;"| When writing a '''Java program''', here is a list of typical errors we are likely to make.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|In a simple  '''Java program''', the  typical errors could be.
  
  
Line 66: Line 66:
 
* Missing semicolon(;)
 
* Missing semicolon(;)
 
* Missing double quotes(" ")
 
* Missing double quotes(" ")
* Mis-match of filename and classname
+
* Mis-match of filename and classname and
 
* Typing the print statement in lower-case
 
* Typing the print statement in lower-case
  
We shall write a program and then make each of these errors and rectify them in '''Eclipse'''
+
We shall write a program and then make each of these errors and see what happens in '''Eclipse'''
  
 
|-
 
|-
Line 93: Line 93:
  
 
'''}'''
 
'''}'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Here we have the Eclipse IDE and the skeleton required for the rest of the code.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Here we have the Eclipse IDE and the project used for the''' HelloWorld''' tutorial 
  
 +
We shall  create  a new  class in the project and use it '''New''' Class'''. Let us name the class ''' Error Free''' and select methods stubs '''public static Void main'''
  
We have created a class '''ErrorFree '''and added the main method to it.
+
Let us minimise the package explorer. Remove the comments and add the print statement to the few errors  
 
+
 
+
We have a HelloWorld program but with a few errors.
+
  
 
|-
 
|-
Line 106: Line 104:
  
  
In this case, the '''System.out''' line has errors and hence we have '''red cross mark.'''
+
In this case, the '''System.out.println  ''' line has an errors and hence their is the '''red cross mark''' on the left.
  
  
Line 117: Line 115:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Pointing at the cross mark on the left of the line '''System.out.println'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Pointing at the cross mark on the left of the line '''System.out.println'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| The first error displayed in the message box says, '''insert semicolon. '''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| The first error displayed is syntax error insert semi-colon to complete block statements
 
+
 
+
 
This is because we have to end every statement of a Java program with a semicolon.  
 
This is because we have to end every statement of a Java program with a semicolon.  
  
Line 127: Line 123:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type '''<nowiki>;</nowiki>'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type '''<nowiki>;</nowiki>'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| So, let us insert the ''semicolon'' at the end of the statement.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| So, let us insert ''semicolon'' at the end of the statement.
  
  
'''Save''' the file.
+
'''Save''' the file with '''Ctrl''' '''s'''.  
 +
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point back to the '''cross mark'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point back to the '''cross mark'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Now we see that there is only one error and the semicolon error has been recolved..
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| notice that once you add a semi-colon and add a file the first error is gone.
 
+
.
 
+
Now, let us look at the other errors in the message box.
+
  
 
|-
 
|-
Line 144: Line 139:
  
  
| style="border:0.035cm solid #000000;padding:0.097cm;"| The second error says that '''HelloWorld cannot be resolved. '''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| their is only one error that says;''' hello world cannot be resolved to a variable, which means to display any message on the console the message has to be included in double quotes.
 
+
 
+
That happens because of missing double quotes.
+
 
+
 
+
To display any message on our console. It should be placed within the double quotes only.  
+
  
  
Line 160: Line 150:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Type “ ” '''on both ends of HelloWorld
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Type “ ” '''on both ends of HelloWorld
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us add double quotes before and after the message, '''HelloWorld.'''
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us add double quotes before and after the message,
  
 
+
''' Ctrl''' s''' to '''Save'''. We see that the red cross mark have gone and the program is error free. So let us ''' run''' the program and see what happens.
'''Save''' the file.
+
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Run the program'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Run the program'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now we see that the error has been resolved and the program is error free.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Run as '''Java applications''  
 
+
 
+
So let us run the program and see what happens.
+
 
+
 
+
Press '''Ctrl + F11''' to run.
+
  
 +
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Output:
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Output:
  
 
'''HelloWorld'''
 
'''HelloWorld'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| We see that the message '''HelloWorld''' is printed on the console.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| We see that the message has been printed on the console.
  
 
|-
 
|-
Line 194: Line 178:
  
  
It does not usually happen on '''Eclipse.'''
+
It does not happens usually on '''Eclipse.'''
  
  
This is because we use the New Class wizard to create a class and  
+
This is because we use the New Class wizard to create a file  and eclipse
  
  
the '''wizard''' creates the file according to the class name.
+
creates a file automatically
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''ErrorFree''' to '''errorfree.'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''ErrorFree''' to '''errorfree.'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| But if we create a '''Java file''' outside of '''Eclipse''' and add it to a project, the error can occur.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| But if we create a '''Java file''' outside of '''Eclipse''' and add it to a project, their is the chance of the error  
  
  
To simulate the error, let us change the class name.
+
So let us stimulate the error , by changing  the class name.
  
  
I’m changing '''E''' to '''e'''
+
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the file '''ErrorFree.java''' in '''Project Explorer Portlet'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the file '''ErrorFree.java''' in '''Project Explorer Portlet'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Since Java is case-sensitive, now the class name and file name are different .  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Since Java is case-sensitive, now the class name and file name do not match .  
  
  
Class name is '''errorfree''' with all letters in lower-case.
 
  
 
While file name is '''ErrorFree.java''' with '''E''' and '''F''' in uppercase.
 
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the '''red cross mark'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the '''red cross mark'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, there is a '''red cross mark '''on the left of the margin.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Notice that , there is a '''red cross mark '''on the left margin.
  
 
|-
 
|-
Line 231: Line 212:
  
  
| style="border:0.035cm solid #000000;padding:0.097cm;"| The message reads '''The public type errorfree must be defined in its own file.'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| And error message reads '''The public type errorfree must be defined in its own file.
  
  
It means, the classname and the filename are different.
+
I
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the class name '''errorfree'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the class name '''errorfree'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Eclipse '''also offers intelligent fixes.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Also notice that the word '''errorfree''' ''' underlined in red colour.
 
+
 
 
+
Notice that the class name is''' underlined in red colour. '''
+
 
+
 
+
Let us mouse over the underline.
+
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the fixes available
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the fixes available
| style="border:0.035cm solid #000000;padding:0.176cm;"| We can see the error message and also that there are '''2 fixes available'''
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| ''' Java offers intelligent fixes  and we have 2 fixes available here 
 
+
  
  
Line 255: Line 228:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the first fix.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the first fix.
| style="border:0.035cm solid #000000;padding:0.176cm;"| The first one is renaming the file to suit the class name.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| The first one is '''rename compilation unit to errorfree java'''
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the second fix.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the second fix.
| style="border:0.035cm solid #000000;padding:0.176cm;"| The second one is to rename the class name to suit the file name.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| The second one is to ''''rename type to errorfree'''.  
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Click '''Rename'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Click '''Rename'''
  
 
Change''' errorfree to ErrorFree'''
 
Change''' errorfree to ErrorFree'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Here, we shall click the second option and rename the class name.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| The fix thatwe are looking to is the second one.And we see that once you rename the file the class back to errorfree the error here is missing.
  
 
As we can see, now the red mark disappears.
 
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change the word '''System '''to''' system'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change the word '''System '''to''' system'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us look at the next error.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| The next error happens due to typing mistakes in the print statement.
 
+
 
+
It happens due to typing mistakes in the print statement.
+
  
  
Line 282: Line 248:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''red cross mark'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to the '''red cross mark'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| We can see that the '''cross mark''' reappears.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| We notice that heir is the ''' red-cross mark'''
  
  
Let us look at the error message. Mouse over the cross mark.
+
And error message reads  '''system cannot be resolved.'''
  
  
The message says '''system cannot be resolved.'''
+
This means Java is expecting a class or object or a  variable by the name '''system.'''
  
  
This means Java is expecting a class or object or variable by the name '''system.'''
+
But there is nothing like system object   in the code.
 
+
 
+
But there is nothing like system object or variable in the code.
+
  
  
Line 302: Line 265:
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the '''system''' word
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Mouse over the '''system''' word
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us look at the possible fixes..
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us look at the possible fixes..
 
 
It says '''11 quick fixes available.'''
 
  
  
Line 314: Line 274:
  
 
Change '''s '''to '''S'''
 
Change '''s '''to '''S'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Out of these, the fix we are looking for is the eighth option.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| there are  '''11  fixes  ''' out of these fix that we are looking for is the eighth option
  
  
It says '''Change to 'System' (java.lang)'''
 
  
 +
  '''Change to 'System' (java.lang)'''
  
So click on it.
 
 
And we see that the program is error free again.
 
  
 +
You can see that once we change it to capital S  the error is missing.
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| This is how we identify errors depending on the error messages and rectify them using '''Eclipse'''.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| here is how you identify errors in Java using eclipse  and rectify them
 
+
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Minimize Eclipse and maximize slides'''
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Minimize Eclipse and maximize slides'''
Line 334: Line 291:
  
 
'''Summary'''  
 
'''Summary'''  
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have come to the end of this tutorial.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial.  
 
+
In this tutorial we have seen
In this tutorial we have learnt
+
 
+
 
'''what are the typical errors while writing a Java program''' and  
 
'''what are the typical errors while writing a Java program''' and  
  
Line 346: Line 301:
  
 
'''Assignment'''
 
'''Assignment'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| As an assignment for this tutorial, identify and fix the errors in the code given below.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| As an assignment for this tutorial, find out the error in the code given below and fix them
  
 
|-
 
|-

Revision as of 23:15, 7 May 2013

Title: Finding Errors and Debugging using Eclipse

Author: TalentSprint

Keywords: Video tutorial, Errors, debugging, eclipse, java


Visual Clue Narration
Slide 1

Welcome

Welcome to the spoken tutorial on Errors and Debugging using Eclipse.
Slide 2

Learning Objectives

In this tutorial, you are going to learn

xwhat are the possible error about the possible errors in a typical Java Program,

how to identify them and

how to correct them using Eclipse.

Slide 3

Software Versions

For this tutorial we are using

Ubuntu 11.10,

Eclipse 3.7

Slide 4

Prerequisites

To follow this tutorial you must know

how to create and run a Java Program in Eclipse.


If not, for relevant tutorial please visit our website as shown [1]http://spoken-tuitorial.org

Slide 5

Possible Errors

  • Missing semicolon(;)
  • Missing double quotes(" ") Mis-match of filename and classname
  • Typing the print statement in lower-case


In a simple Java program, the typical errors could be.


{Pause for a few seconds}

  • Missing semicolon(;)
  • Missing double quotes(" ")
  • Mis-match of filename and classname and
  • Typing the print statement in lower-case

We shall write a program and then make each of these errors and see what happens in Eclipse

Minimize slides and maximize Eclipse

{Eclipse should contain the project DemoProject used in previous tutorial}


And the file ErrorFree.java with these lines of code.


public class ErrorFree

{

public static void main(String args[])

{

System.out.println(Hello World)

}

}

Here we have the Eclipse IDE and the project used for the HelloWorld tutorial

We shall create a new class in the project and use it New Class. Let us name the class Error Free and select methods stubs public static Void main

Let us minimise the package explorer. Remove the comments and add the print statement to the few errors 
Point to the red cross mark on the margin, left of the line Sytem.out... In Eclipse, the line which has the error will be indicated with a red cross mark on the left margin.


In this case, the System.out.println line has an errors and hence their is the red cross mark on the left.



Hover mouse over the cross mark. The list of errors is displayed by hovering the mouse over the cross mark.
Pointing at the cross mark on the left of the line System.out.println The first error displayed is syntax error insert semi-colon to complete block statements

This is because we have to end every statement of a Java program with a semicolon.



Type ; So, let us insert semicolon at the end of the statement.


Save the file with Ctrl s.


Point back to the cross mark notice that once you add a semi-colon and add a file the first error is gone.
.
Pointing to the second error.


their is only one error that says; hello world cannot be resolved to a variable, which means to display any message on the console the message has to be included in double quotes.


Without the quotes, Java thinks that HelloWorld is the name of a variable.



Type “ ” on both ends of HelloWorld Let us add double quotes before and after the message,

Ctrl s to Save. We see that the red cross mark have gone and the program is error free. So let us run the program and see what happens.

Run the program Run as 'Java applications


Output:

HelloWorld

We see that the message has been printed on the console.
Open the New Java Class Wizard.


Type the class name and Finish

Point to the created file name and class name.

Let us look at the next error.


It happens due to a mismatch of file name and class name.


It does not happens usually on Eclipse.


This is because we use the New Class wizard to create a file and eclipse


creates a file automatically

Change ErrorFree to errorfree. But if we create a Java file outside of Eclipse and add it to a project, their is the chance of the error


So let us stimulate the error , by changing the class name.



Point to the file ErrorFree.java in Project Explorer Portlet Since Java is case-sensitive, now the class name and file name do not match .



Mouse over the red cross mark Notice that , there is a red cross mark on the left margin.
Mouse over the error message


And error message reads The public type errorfree must be defined in its own file.


I

Mouse over the class name errorfree Also notice that the word errorfree underlined in red colour.
Mouse over the fixes available Java offers intelligent fixes and we have 2 fixes available here


Point to the first fix. The first one is rename compilation unit to errorfree java
Point to the second fix. The second one is to 'rename type to errorfree.
Click Rename

Change errorfree to ErrorFree

The fix thatwe are looking to is the second one.And we see that once you rename the file the class back to errorfree the error here is missing.


Change the word System to system The next error happens due to typing mistakes in the print statement.


Let us replace the capital S with a small s.

Point to the red cross mark We notice that heir is the red-cross mark


And error message reads  system cannot be resolved.


This means Java is expecting a class or object or a variable by the name system.


But there is nothing like system object in the code.



Mouse over the system word Let us look at the possible fixes..



Click on Change to 'System' (java.lang)


Change s to S

there are 11 fixes out of these fix that we are looking for is the eighth option


 Change to 'System' (java.lang)


You can see that once we change it to capital S the error is missing.

here is how you identify errors in Java using eclipse and rectify them
Minimize Eclipse and maximize slides

Slide 6

Summary

This brings us to the end of this tutorial.

In this tutorial we have seen what are the typical errors while writing a Java program and

how to identify and rectify them using Eclipse.

Slide 7

Assignment

As an assignment for this tutorial, find out the error in the code given below and fix them
Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


To know more about the Spoken Tutorial project,

Watch the video available at the following link, that summarises the project.Alternatively, you can download and watch it.

Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials.


Gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.

Slide 10Acknowledgement
  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Spoken Tutorial Project is a part of the Talk to a Teacher project.


Itis supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro

Slide 11About the contributor
  • This tutorial has been contributed by TalentSprint
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint.

Thanks for joining.



Contributors and Content Editors

Chandrika, PoojaMoolya, Pravin1389, Sneha