Sunday, November 30, 2008

Published 11/30/2008 by with 3 comments

class [ClassName] is public, should be declared in a file named [ClassName].java

We get error message "class [ClassName] is public, should be declared in a file named [ClassName].java" if the file name of the Java source program is not same as the public class name.

Solution is to rename the file or the class name to make them same. If a public class name is ABC then the file name of that class must be ABC.java. So always copy the class name and paste it when naming the file, then we will not face this error anymore.
Read More
    email this       edit

Tuesday, November 18, 2008

Published 11/18/2008 by with 0 comment

Exception in thread "main" java.lang.NoSuchMethodError: main

'Exception in thread "main" java.lang.NoSuchMethodError: main' is a run time error message. If the main method is not properly defined, we may see this error message. You know if you want to execute/run a program that program must have a main method of the following signature:

public static void main(String args[])

Generally we do the following mistakes in this regard:

1. May be, you haven't defined the main method at all. Define it at first then run it.

2. The method name is not correctly written. The spelling will be exactly in lower case 'main', if we write 'Main' - it will be an error.

For example:

public class NoSuchMethodErrorMain
{
public static void Main(String args) // M is capital here
{
System.out.println("Java is an Object Oriented Language");
}

}




3. The keyword 'static' is omitted. The main method must be static.

For example,

public class NoSuchMethodErrorMain
{
public void main(String args) // keyword static is missing
{
System.out.println("Java is an Object Oriented Language");
}

}



4. The parameter is not declared. Java main method must have a String array parameter. Sometimes, we forget to put [] after the data type or the parameter name (args, here).

For example:

public class NoSuchMethodErrorMain
{
public static void main() // parameter problem
{
System.out.println("Java is an Object Oriented Language");
}

}


OR

public class NoSuchMethodErrorMain
{
public static void main(String args) // the parameter is not array
{
System.out.println("Java is an Object Oriented Language");
}

}


The correct code will be:

public class NoSuchMethodErrorMainSolution
{
public static void main(String args[])
{
System.out.println("Java is an Object Oriented Language");
}

}
Read More
    email this       edit
Published 11/18/2008 by with 0 comment

is already defined

If we declare a variable in a method more than once than we see ' is already defined'. Find out the variable for which the message is shown and check whether you have declared the variable more than one time in a particular block.

If we compile the following code we will see 'country is already defined in myMethod()'
public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
String country="Bangladesh";// declared again
System.out.println(country);

}

}


Now the solution is to remove the declaration 'String' from the assignment statement.

public class AlreadyDefinedSolution
{
public void myMethod()
{
String country;
country="Bangladesh";//declaration removed
System.out.println(country);

}

}


Read More
    email this       edit
Published 11/18/2008 by with 1 comment

reached end of file while parsing

The error 'reached end of file while parsing' occurs because of curly braces } problem. You have given either more or less curly braces } than the required number of } braces.

See the code below:
public class ReachedEndOfFileWhileParsing
{
public static void main(String args[])
{
System.out.println("java-error-massages.blogspot.com");

}

Here two curly braces are started but only one is ended. We need to put another } after the display statement here like below:

public class ReachedEndOfFileWhileParsingSolution
{
public static void main(String args[])
{
System.out.println("java-error-massages.blogspot.com");
}// it is now given

}
Read More
    email this       edit

Sunday, November 9, 2008

Published 11/09/2008 by with 2 comments

possible loss of precision

You may see the error message 'possible loss of precision' for any of the following reasons:

1) You are trying to assign a fractional number into a float variable(why not?). Actually Java compiler considers the fractional number as double by default. You need to explicitly specify the fractional number as float by giving the character 'f' or 'F' after the the number, like float number = 65.37F;

2) Or you are trying to assign the value of a double variable into a float variable. Memory size of double is 8 byte and that of float is 4 byte. If you assign a double value into a float value data may be lost. So be sure about the data type. If you are sure you want to assign the double into float, you need to cast it appropriately, like float floatVariable = (float)doubleVariable;

An example is given below:

public class PossibleLossOfPrecision
{
public static void main(String args[])
{

float floatNumber1=65.37; // error
double doubleNumber=65.37;
float floatNumber2=doubleNumber; // error

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}


The above code is wrong; write this code as below and the problem will be solved.

public class PossibleLossOfPrecisionSolution
{
public static void main(String args[])
{

float floatNumber1=65.37F; // error solved
double doubleNumber=65.37;
float floatNumber2=(float)doubleNumber; // error solved

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}
Read More
    email this       edit
Published 11/09/2008 by with 1 comment

integer number too large

We know that the maximum value of an int is 2147483647. If we write any whole number more than 2147483647 in our Java program, we may get this error message 'integer number too large' but may be you are trying to store this large number in a long type variable, then what to do? The solution is to put the letter 'l' or 'L' after the number. This happens, because the compiler considers the whole number as int by default and we specify the large number by character 'l' or 'L' as long number. Now the compiler will consider the number as long.

For example, we are trying to compile the following Java code:
public class IntegerNumberTooLarge
{
public static void main(String args[])
{
long number=965478213245;
System.out.println(number);
}
}

We will get 'integer number too large' from the above program. Now write the program as below the problem will be solved.

public class IntegerNumberTooLargeSolution
{
public static void main(String args[])
{
long number=965478213245L;
System.out.println(number);
}
}
Read More
    email this       edit

Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

; (semi-colon) expected

Each java statement must ends with ; (semicolon) in Java. If we don’t put ; after any statement we will see ‘;’ expected

Here is an example of this error:
public class SemicolonExpected
{
public static void main(String args[])
{
int x=10,y=10 //no ; (semicolon) is given after this statement
if(x==y)
System.out.println(x +" and "+y+" is equal");
}
}

Here no ; (semicolon) is given after the x,y declaration statement. The correct code is:

public class SemicolonExpectedSolution
{
public static void main(String args[])
{
int x=10,y=10; //; (semicolon) is given now
if(x==y)
System.out.println(x +" and "+y+" is equal");
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

variable … might not have been initialized

We see “variable might not have been initialized” when we try to use the value of a variable before assigning any value to that variable. Each local variable must be assigned a value before further using it.

Here is an example of this error :
public class VariableMightNotHaveBeenInitialized
{
public static void main(String args[])
{
int a,b,c;
a=10;
c=a+b;
System.out.println("The sum is "+c);
}
}


Here variable ‘b’ is added with variable ‘a’ before assigning any value.

Correct code is:
public class VariableMightNotHaveBeenInitializedSolution
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println("The sum is "+c);
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

missing method body, or declare abstract

If we put a ; (semicolon) after the method header (i.e., before starting the method body) when we are defining a concrete method (which method has body) then we will see “missing method body, or declare abstract”. Another reason is if we don’t write the keyword ‘abstract’ for an abstract method (a method without body), we’ll also see this message.
To resolve the problem we must remove the ; (semicolon) from the header of the concrete method or if it is an abstract method the keyword ‘abstract’ must be written in the method header.

Here is an example of this error:
public abstract class MissingMethodBodyOrDeclareAbstract
{
public void display(String message);
{
System.out.println(message);
}
public void display();
}

Here, a ; (semicolon) is given after the method header of the first display method (with parameter). And for the second display method, keyword ‘abstract’ is not stated in the header and it doesn’t have any body also. Here it is to be noted that if a class contains an abstract method, the class must also be abstract.

The correct code is:

public abstract class MissingMethodBodyOrDeclareAbstractSolution
{
public void display(String message)
{
System.out.println(message);
}
public abstract void display();
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

else without if

Programmers may see message “else without if” when the selection (if ... else if ... else) structure is used. This may happen for any of the following reasons:
Semicolon (;) is given after the last parenthesis of the condition
Error in use of the curly braces ( } ), may be the } is not closed before starting the next else if or the else or } is given more.

Here is an example of this error:
public class ElseWithoutIf
{
public static void main(String args[])
{
int a=10,b=5;
if(a==b);
{
System.out.println(a+" is not equal to "+b);
else
{
System.out.println(a+" is equal "+b);
}
}
}


Here we have given ; (semicolon) at the last of the first condition, it’s an error. Again, we haven’t closed the } of the if i.e., else is written within the ‘ if’. So we need to remove the ; and put another } before the else.

Now the correct code is:
public class ElseWithoutIfSolution
{
public static void main(String args[])
{
int a=10,b=5;
if(a==b)
{
System.out.println(a+" is not equal to "+b);
}
else
{
System.out.println(a+" is equal "+b);
}
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

cannot find symbol / cannot resolve symbol

“cannot find symbol” or “cannot resolve symbol” is the most common error message which the programmers get from the compiler. However this error message may mean that the variable/method/class/object used are not declared/defined properly. In most cases, this happens because:

· The variable/object is not declared or the spelling of variable/object declaration & later use of that variable/object is not same.

· The method is not defined or the spelling of the method definition and method calling is not same or the number and/or type of parameters are wrongly given when calling the method or the method where it is defined is not imported properly (in case of class library methods, the package where the method is defined is not imported)

· If the message is shown for a class, the package of that class might not have been imported or the spelling is wrong.

The solution of that problem is to check the spelling at first, Java programmers see this message most often just because of the wrong spelling( be careful about the case also, as Java is a case sensitive language). If the spelling is correct check whether the required package is imported.

Here is an example of this error :

public class CannotFindSymbol
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print(“Enter the quantity: ”);
quntity=imput.nextInteger();
System.out.println(“Your number is ”+quantity);
}
}


In the above code, there are many lines where we will get this error message.

At first it will say Scanner cannot be resolved/found because the package of this class java.util is not imported.

Also problem in ‘quntity’ as the variable is declared as ‘quantity’

Error in ‘imput’ as the object is created as ‘input’

If you correct all the errors it will show error message for nextIntger as the actual method name is nextInt in class Scanner

Now the correct code is:

import java.util.*;
public class CannotFindSymbolSolution
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print("Enter the quantity: ");
quantity=input.nextInt();
System.out.println("Your number is "+quantity);
}
}
Read More
    email this       edit