Monday, October 8, 2007

Common Errors during JAVA programing..........

Hi friends........
I have completed about one and half month at CDAC-B. In all those days I have done lot of programming in JAVA and DATA-STRUCTURE. During programming I found lots of compile time and run time errors. Because I was not familier with Java, so I was facing lot of difficulty during my initial days.Whenever I was getting an error I tried to solve by one of my friends. Today I want to discuss some common errors which I did all the times.
> 1 : Compile-time errors.......
During compilation compiler only checked syntax error. Compiler does not find the logical error.Compiler generate error massage for small mistake like semicolon,braces mismatch, checking int value in "IF" condition instead of boolean value, unbalanced parentheses, unclosed literals, illegal start of expression, not a statement, misspelling "else", cannot find symbol, is already defind, array required but....found, variable.....might not have been initialized, ....in...cannot be applied to..., operator.... cannot be applied to....., possible loss of precision, incompatible types, inconvertible types, missing return statement.., missing return value, cannot return a value from method whose return type is void, invalid method declaration....return type required, unreachable statement,non-static variable.....cannot be referenced from a static context, non-static method...cannot be referenced from a static contextm,
etc...
It's a long list of compiler errors which I got most of time. I am
giving some example which generated compiler error.
:> class ClassName{
void f(){
int n=10;
// Error, closing brace is missing
void g(){
int m=20;
}
}
:> if(i > j // error, unclosed parentheses
max = i // error, missing semicolon
else
max=j;
:> String myString=
" This is My First Java Programm "+
" and I got compilation error; // error, unclosed literal
:> int max;
..........
......
max; // error, missing =
:> if( i > j)
max = i;
els // error, else not spelled correctly
max = j;
:> boolean b=true;
int x = (int) b; // error, can't convert bpplean to int

> 2 : Runtime Errors......
When the JAVA interpreter encounters an error during runtime it throws an exception and prints a stack trace showing the call stack -the list of methods called from the main program untill the statement that caused the exception.
I am taking an example to clear concept....
:> class RunTimeTest{
public static void main(String arg[]){
String s = "Hello World";
System.out.println(s.substring(10,12);
}
}
Generated Error as:
Exception in thread "main"
java.lang.StringOutBoundException:
String index out of range: 12
at java.lang.String.substring(Unknown source)
at RunTimeTest.main(RunTimeTest.java:4)

other kind of runtime errors might also be possible are as....
> out of range :
> ArrayIndexOutOfRange
> StringIndexOutOfRange
> NullPointerException
> InputMismatchException
> IllegalFormatException
> NumberFormatException
> ArithmeticException : This exception will generate if we
divide any no. by Zero(0).
> outOfMemoryError
> StackOverFlowArea
> During execution of program sometimes we got error like
this : NoClassDefFoundError
> NoSuchMethodFoundError : main
this error is generated when we forget to write
public with main i.e.
void main(String arg[]){.....}

> 3 : Assignment and Equality......

> String equality
sometimes we forget to compared string with method "equals".
> Assignment of references ....
one important thing about assignment of references is
when we assign references it only assign reference not the content of object.

These are some common errors which I noticed from when I started programing in java.

No comments:

Post a Comment