Wednesday, May 20, 2009

Published 5/20/2009 by with 40 comments

package system does not exist

Very simple error ‘package system does not exist’- most probably you have used ‘err’ or ‘in’ or ‘out’ field of the ‘System’ class of ‘java.lang’ package but you have given lower case "s" instead of upper case "S" in the class name System. Remember Java is case sensitive.

See the below code:

system.out.println("package system does not exist error"); // here s is in lower case


But in the following code:

System.out.println("package system does not exist error solved");//S is in upper case

Though System is not a package, as you have written system.out.println – three parts here (system, out and println) a package like statement, Java compiler considers it as package but as this package doesn’t exist, the error message is shown.

So the solution is just change the lower case "s" of system to upper case "S".
Read More
    email this       edit

Sunday, May 17, 2009

Published 5/17/2009 by with 1 comment

non-static method ... cannot be referenced from a static context

If you try to access a non static member of a class from a static context, you will see ‘non-static method … cannot be referenced from a static context’. For example, you might have tried to call a non static method from a static method. Bear in mind, you can access a static member (variables, methods etc) from both static and non-static context but cannot access non-static member from a static context.

See the following example code:

public class NonStaticMethodCannotBeReferencedFromAStaticContext
{
private double firstNumber;
private double secondNumber;
private double result;

public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}

public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}

public double add()
{
return firstNumber+secondNumber;
}

public double substract()
{
return firstNumber-secondNumber;
}


public static void main(String args[])
{
setFirstNumber(10.35);// this is a non-static method but referenced from static main method
setSecondNubmer(20.97);
System.out.println("The addition is "+add());
}

}

For the solution you can:
i) declare the non-static member as static and access them from the static context (not always; be careful, this may create other problems)
ii) Create an instance of the class where the non-static members are and then reference the non-static members by the created instance or object.

Browse http://javatech-stuff.blogspot.com/2007/11/differences-between-java-terms.html for understanding the differences between static and non-static member.

Now see the solution below:

public class NonStaticMethodCannotBeReferencedFromAStaticContextSolved
{
private double firstNumber;
private double secondNumber;
private double result;

public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}

public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}

public double add()
{
return firstNumber+secondNumber;
}

public double substract()
{
return firstNumber-secondNumber;
}


public static void main(String args[])
{
NonStaticMethodCannotBeReferencedFromAStaticContextSolved object=new NonStaticMethodCannotBeReferencedFromAStaticContextSolved();
object.setFirstNumber(10.35);// accessed by the instance named object
object.setSecondNubmer(20.97);
System.out.println("The addition is "+object.add());
}

}
Read More
    email this       edit
Published 5/17/2009 by with 0 comment

illegal start of expression

If a method is defined inside another method we may see ‘illegal start of expression’ error message. Remember that, we cannot define a method inside another method; we just can call a method inside another method. Sometimes it occurs just because of improper opening and closing of curly braces ( { or } ).

See the following example code:
public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");

/* methodB is defined inside methodA because we didn’t close methodA before defining methodB*/

public void methodB()
{
System.out.println("This is method B");
}
}
}

Now see the solution:

public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");
}

/* methodA is finished then, methodB is being defined*/
public void methodB()
{
System.out.println("This is method B");
}

}
Read More
    email this       edit
Published 5/17/2009 by with 2 comments

identifier expected

This error message is shown when the statements are not written in proper place. Most often, mistakenly, we may write the processing task (for example assigning value to a variable; writing a loop etc.) outside of any method. In this situation we may see this type of error message.

Then the solution is to write such statements inside the appropriate methods.

See the example code below:
public class IdentifierExptected
{
int number1,number1,sum;


number1=10; // identifier expected
number2=20; // identifier expected
sum=number1+number2; // identifier expected

/*
The assignment statement must be written inside a method. Variables can also be assigned/ initialized during the declaration.
*/


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}


}

See the solution now:

public class IdentifierExptectedSolved
{
int number1=10,number2=20,sum;


public void sum()
{
sum=number1+number2;
}


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}



}
Read More
    email this       edit

Monday, December 1, 2008

Published 12/01/2008 by with 1 comment

Exception in thread "main" java.lang.NoClassDefFoundError: [ClassName]

If a class file does not exist or the class is in such a package which is not in the CLASSPATH but you are trying to run that class, then you may see "Exception in thread "main" java.lang.NoClassDefFoundError: [ClassName]".

Sometimes, by mistake, we(the students) try to execute a Java program without compiling it successfully. If a Java class is not compiled successfully, no class file (a file with extension class) is generated. So we need to compile the Java program at first, then it should be run.

The class might be in another directory or package. The class must be in the CLASSPATH for the successful running.

In another situation along with the above error message you may see "CMD.EXE was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory" and "CMD does not support UNC paths as current directories". You may see this when you run a program by a batch file or by using TextPad etc. This error means you are running a class which is located in a remote machine and you have given the address of the class like \\RemotePC\directory\ClassName. You should bring the class to your PC (if required) to run it. Or if you want to call a class from the remote PC you may use RMI.
Read More
    email this       edit

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