Page 1 of 1

Beginners, Fix Your own Java Errors top 10 beginner java run time and comiler errors and how to fix them Rate Topic: ***** 2 Votes

#1 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Posted 08 April 2010 - 08:29 AM

*
POPULAR

Java Errors, A Beginners Guide
Ok, so your a new programmer and either by your own interest or your teacher/professor's decision you chose Java to work with. Now any programmer (especially a new one) is bound to make mistakes, and Java is there to tell you what you did wrong. But what if you dont understand what Java is telling you? It wouldn't be much help then. So lets go through a few together, and really see what they mean or how to fix them.


1) File Not Found
C:>javac Test.java
javac: file not found: Test.java
Usage: javac <options> <source files>
use -help for a list of possible options



This is a pretty simple one, in this case i tried to compile Test.java, however I never created it so this is java's way of telling me that I need to re-enter the file again, either spelled correctly or with the right path


2) java.lang.NoClassDefFoundError: Test
C:>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)



This is similar to issue number 1. In this scenario I just tried to run java on some program named Test (i never created or compiled it, just called it). Java is saying "I cannot find Test.class." Usually this is caused by you forgetting to compile your Test.java (or if it failed to compile), or you misspelled the program name. Yes misspelling will probably show up on most of these errors but you'd be amazed how often its the culprit.


2)a] java.lang.NoClassDefFoundError: Test/class
C:>java Test.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test/class
Caused by: java.lang.ClassNotFoundException: Test.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)



I called this 2a since its very close except for the "/class" at the end of the first line. This error happense when you try and run you program with including the ".class" in the command it should be run as java Test not java Test.class


3) Exception in thread "main" java.lang.NoSuchMethodError: main
What java is saying here is "I cannot find main", now main is quite important but that is the entry point to all java programs. Every java program needs 1 and only 1,so don't forget it:
public class Test{

  //This is the main method. do cool stuff here
  public static void main( String[] args ){
  
  }
}




4) reached end of file while parsing
Here is a real simple one to fix. This means that you forgot a close brace '}' somewhere in your code so that when it reaches the end of your file, the class open/close braces is not complete. A simple example is this:
import javax.swing.*;
public class Test
{
	public static void main (String[] args)
	{
		System.out.println("Hello World");
	}
 //notice lack of } here



5) ';' expected
Once again another short and easy one, in full it will look something like this:
C:>javac Test.java
Test.java:6: ';' expected
                System.out.println("Hello World")


This is when you forget a semicolon ';' at the end of your statement/command. Java compiler usually tells you the line your pretty much need to fix, or the line after, either way, if you see the error, go hunting for semi colons


6) cannot find symbol
looks like:
Test.java:6: cannot find symbol
symbol  : class ArrayList
location: class Test
                ArrayList<String> a = new ArrayList<String>();


Theres a lot of reasons how to generate this error, but luckily its not too hard to fix. Lets see how I can generate it
public class Test
{
	public static void main (String[] args)
	{
		ArrayList<String> a = new ArrayList<String>();
	}
}


In the case above, I wanted to use an ArrayList, that is not a simple object, in fact it is in a package of other classes called java.util. anyway, without importing that package my compiler had no idea what an ArrayList was. In this case the solution is to add an import statement
 import java.util.*; 


The second major cause of this error is spelling, capitalization, and old naming mistakes. by old naming mistakes, i mean. you started out using some naming convention for a variable then you went back and changed it, but you for got to change them all. Here is all the examples:
int myApples, myPears, myPeaches;

myapples = 0; //Error, check your capitalization
myPearrs = 0; //Error, check your spelling

//many days later... for the sake of forgetfulness 
Peaches = 0;  //Error, you forgot 'my' either cause
              //you tried to change the var or forgot



7) YourClass.java:##: incompatible types
C:>javac Test.java
Test.java:4: incompatible types
found   : int
required: java.lang.String
                String a = 5;
                           ^


This error happens a lot, it occurs when you try and store a value into a variable, but the variable is a different type than the value. As seen above i try and pass an int to a String since these are not the same, an error was thrown.
*Take note that some variables will automatically be converted, for example double d = 5; will work however int d = 5.0; will not.
The key to fixing this error is to make sure you properly reformat your value. In the example there are three basic ways to fix this issue: String a = 5 + "" (concat an empty string), String a = "5" (pass a string), or
Integer i = new Integer( 5 );
String a = i.toString();


You can use the Integer object, and since all objects have a toString method, you can use that method. In my other example i said that int i = 5.0 would fail. However the last trick we can do to fix this error is called casting. casting will remove the extra data and allow it to work. when going from a double (5.0) to an integer( 5 ) the process is called truncating. This is not rounding!!! it is literally chopping off the '.0' it looks like this int a = (int)5.0;. Take note even if the number is 5.999999 the int will still be 5.

8) cannot be applied to
C:>javac Test.java
Test.java:5: foo(double) in Test cannot be applied to (java.lang.String)
                foo( s );
                ^



That error is generated by this code:
public class Test{
	public static void main( String args[] ){
		System.out.println( "Hello World");
		String s = "";
		foo( s );
		
	}
	public static String foo( double a ){
		return a+"";
	}
}



Whats happening is my method foo is expecting a double variable to be passed to it, however I tried to pass a String instead. Java is simply saying that it cannot find foo( String a ), it can only find foo( double a ).
It doesn't just say invalid parameter and leave it at that because Java supports method overloading, so in the event that I did right the method foo( String a ), it would be able to handle it.

9) java.lang.OutOfMemoryError: Java heap space
C:>java Test
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.lang.Integer.valueOf(Integer.java:585)
        at Test.main(Test.java:10)




The code that generates this:
import java.util.ArrayList;
public class Test{
	public static void main( String args[] ){
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		int i = 0;
		while(true){
			numbers.add( i++ );
		}
	}
}



What went wrong here is the Java Virtual Machine (JVM) ran out of memory. In this case the ArrayList got too big to the point there was no more space left to do computations. Since the loop will continue until the while condition is evaluated as false, the true in there will make it an infinite loop. There are many other ways to make an infinite loop, however that is generally where you are going to want to look if you get this error. For some reason your boolean statement is not ever returning false. A good practice to avoid is putting any variable that you will be modifying inside the loop, inside the boolean condition as well. This way you will have no opportunity to modify it.

Take note that this ifnite will not throw the error:
while(true){
	Object o =new Object();	
}


This is because the JVM's Garbage collector is deleting 'o' at the end of the loop, since there is no more reference to it once it hits the '}' (this is called leaving scope). The reason the ArrayList throws the error, is the collection maintains the reference, thereby not giving the garbage collector an opportunity to free up the memory.

10) class, interface, or enum expected
C:>javac Test.java
Test.java:2: class, interface, or enum expected
int i;
^
1 error



The code that generates this:
//beginning of file
int i;

public class Test{
	public Test(){
		i = 0;
	}
}
//end of file



I see this issue quite often with brand new programmers, since they don't understand structure of class files. This error is saying that the int i that was created isn't a part of any namespace. Therefore there is no way for the JVM to get at it. The same goes for any methods or constructors that that you put outside the class definition, they too will throw the error. The fix is simple, just bring whatever is throwing the error inside of the first open brace inside your program (aka after public class Test{).

This post has been edited by SwiftStriker00: 09 April 2010 - 06:53 AM


Is This A Good Question/Topic? 21
  • +

Replies To: Beginners, Fix Your own Java Errors

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Posted 08 April 2010 - 06:29 PM

Very good tutorial. I'll be sure to keep this bookmarked to direct newbie users to.
Was This Post Helpful? 1
  • +
  • -

#3 prac87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-July 10

Posted 25 July 2010 - 07:26 PM

thanks a lot buddy
Was This Post Helpful? 0
  • +
  • -

#4 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

Posted 27 July 2010 - 07:53 AM

dude, you TOTALLY SAVED ME. All the time the prof was just saying things like oh, this error, i see i know where to fix, and i have no idea what he talking about.
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Posted 02 August 2010 - 07:20 AM

Very nice. Fix your own errors, bitch. Lol.
Was This Post Helpful? 1
  • +
  • -

#6 Ebiz1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 11

Posted 12 July 2011 - 03:00 PM

Very nice tutorial, a lot of time can be saved. Good job
Cheers
Was This Post Helpful? 0
  • +
  • -

#7 jaffa293  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-July 11

Posted 19 July 2011 - 04:21 AM

very helpful.
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 biologicalprogrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-May 13

Posted 03 May 2013 - 10:10 PM

View Postvienhuynh, on 27 July 2010 - 07:53 AM, said:

dude, you TOTALLY SAVED ME. All the time the prof was just saying things like oh, this error, i see i know where to fix, and i have no idea what he talking about.

I was in the same boat too lol this tut iz amazing
Was This Post Helpful? 0
  • +
  • -

#9 dvs.amy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-October 12

Posted 08 May 2013 - 09:05 PM

Thank you so much!! I don't know how many times I have made minor errors like typos or no closing brace and been completely thrown off because I didn't know what the error messages translated to!! :clap:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1