Results 1 to 19 of 19
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Thumbs up [SOLVED] Exception in thread "main"

    Okay, i dont usually use windows, but i decided to use it for my programming for some reason. Ill probably convert back once i get into it more, but im studying java from a book, now i copied out the code listing exactly and re-checked it, however i keep recieving the same error.

    Java Code:
    Exception in thread "main" java.util.NoSuchElemen
            at java.util.StringTokenizer.nextToken(Un
            at TokenTester.main(TokenTester.java:19)
    Now here is the Java code from this small program:
    Java Code:
    import java.util.StringTokenizer;
    
    class TokenTester
    {
    	public static void main(String[] args)
    	{
    		StringTokenizer st1, st2;
    		
    		String quote1 = "VIZY 3 -1/16";
    		st1 = new StringTokenizer(quote1);
    		System.out.println("Token 1: " + st1.nextToken());
    		System.out.println("Token 2: " + st1.nextToken());
    		System.out.println("Token 3: " + st1.nextToken());
    		
    		String quote2 = "NPLI@9 27/32@3/32";
    		st2 = new StringTokenizer(quote2);
    		System.out.println("\nToken 1: " + st2.nextToken());
    		System.out.println("Token 2: " + st2.nextToken());
    		System.out.println("Token 3: " + st2.nextToken());
    	}
    }

    Compiles fine, but when i run it, it fails.

    According to the book the outcome is supposed to be:
    Java Code:
    Token 1: VIZY
    Token 2: 3
    Token 3: -1/16
    
    Token 1: NPLI
    Token 2: 9 27/32
    Token 3: 3/32
    I'd really appreciate any help on this error. Thanks
    Last edited by Nakira; 10-28-2008 at 11:18 PM. Reason: Solved-ish

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Can you post the entire error message? Wrap it around if you need too.

  3. #3
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: TokenTester
    Caused by: java.lang.ClassNotFoundException: TokenTester
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Is this the entire code or was this taken from a larger section? If this is all of it you need to declare TokenTester as public class TokenTester and have your file named TokenTester.java.

    Also I'm not sure what all the (Unknown Source) errors are from. I think it's not able to resolve the java.util.StringTokenizer; for some reason. Maybe someone else knows the answer to this.

  5. #5
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Usually if you declare a class and dont state provate or public etc its auto public, but ill change it anyway to see if it helps any. Im really boggled with these errors...


    ...Just changed it, still the same errors..
    Last edited by Nakira; 10-28-2008 at 06:41 PM. Reason: Updated post

  6. #6
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    I'm getting a similar error again, im wondering if java is stopping me import the files. Here is the error, Followed by the code:

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: PointSetter
    Caused by: java.lang.ClassNotFoundException: PointSetter
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    Java code:
    Java Code:
    import java.awt.Point;
    
    public class PointSetter
    {
    	public static void main(String[] args)
    	{
    		Point location = new Point(4, 13);
    		
    		System.out.println("Starting location:");
    		System.out.println("X equals " + location.x);
    		System.out.println("Y equals " + location.y);
    		
    		System.out.println("\nMoving to (7, 6)");
    		location.x = 7;
    		location.y = 6;
    		
    		System.out.println("\nEnding location:");
    		System.out.println("X equals " + location.x);
    		System.out.println("Y equals " + location.y);
    	}
    
    	}
    There is nothing wrong with the code, but java complains about something. Haha, Hopefully somebody can help me?...

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    My first assumption was that java couldn't extract the imports because there was no internet connection but I realized it shouldn't have too, they should be stored within the JRE.

    Have you tried doing a simple "Hello World" program to see if it's not actually your IDE messing up?

  8. #8
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    I'll have a go now. One second.


    Haha, yeah seems like my IDE has screwed up. Not sure what im gonna have to do to sort it. Should i reset my path and classpath variables?
    Last edited by Nakira; 10-28-2008 at 06:59 PM. Reason: Updated post

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You mentioned your programming in Windows even though you don't normally use it. Does it run on your other OS? You might try reinstalling Notepad++ and resetting your paths. I had a similar thing happen in Eclipse. I pulled a project from a flashdrive and it changed my workspace to that drive so nothing ran unless the flashdrive was plugged in.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Here is what happens when I enter: java <classname> for a class file that is not in the folder:
    D:\JavaDevelopment\Testing>java asdf
    Exception in thread "main" java.lang.NoClassDefFoundError: asdf
    This was done in a command prompt window.

    Was the PointSetter.class file in the folder/directory where you entered the following command:
    java PointSetter

    How are you trying to execute the program?

  11. #11
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Here is what happens when I enter: java <classname> for a class file that is not in the folder:

    This was done in a command prompt window.

    Was the PointSetter.class file in the folder/directory where you entered the following command:
    java PointSetter

    How are you trying to execute the program?
    Hey, yeah all of the necessary files are in the folder that im running it from, im using javac filename.java and then java filename, which is what your meant to do to compile and run them.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    If all the files are in the same folder and all the commands are issued in that folder and if the the javac creates a .class file, then the java command should execute the class file.
    Can you do a dir command in the folder then do the java command and copy and post all of the contents of the command prompt window here?
    It should work.

    Use the icon in the upper left of the command prompt window to mark and copy the contents of the window.

  13. #13
    jyotigupta6 is offline Member
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default Re :

    When I am executing the same program, I am getting this output -

    Token 1: VIZY
    Token 2: 3
    Token 3: -1/16

    Token 1: NPLI@9
    Token 2: 27/32@3/32
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at TokenTester.main(TokenTester.java:20)

  14. #14
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Found 1 error in the code, here it is revised, but it makes no differnece!!
    Java Code:
    import java.util.StringTokenizer;
    
    class TokenTester
    {
    	public static void main(String[] args)
    	{
    		StringTokenizer st1, st2;
    		
    		String quote1 = "VIZY 3 -1/16";
    		st1 = new StringTokenizer(quote1);
    		System.out.println("Token 1: " + st1.nextToken());
    		System.out.println("Token 2: " + st1.nextToken());
    		System.out.println("Token 3: " + st1.nextToken());
    		
    		String quote2 = "NPLI@9 27/32@3/32";
    		st2 = new StringTokenizer(quote2, "@");
    		System.out.println("\nToken 1: " + st2.nextToken());
    		System.out.println("Token 2: " + st2.nextToken());
    		System.out.println("Token 3: " + st2.nextToken());
    	}
    }

    Dir cmd:
    Java Code:
    28/10/2008  17:57    <DIR>          .
    28/10/2008  17:57    <DIR>          ..
    28/10/2008  18:04               410 HW.class
    28/10/2008  17:57               108 HW.java
    28/10/2008  15:31               958 investment.class
    28/10/2008  15:31               479 investment.java
    28/10/2008  17:47               953 PointSetter.class
    28/10/2008  17:48               527 PointSetter.java
    28/10/2008  15:21               362 testInvestment.class
    28/10/2008  15:21               265 testInvestment.java
    28/10/2008  17:41             1,003 TokenTester.class
    28/10/2008  17:41               615 TokenTester.java
    28/10/2008  15:43               589 TokenTester.java.bak
    25/10/2008  14:01               284 Variables.java
    22/10/2008  19:21               836 VolcanoApplication.class
    22/10/2008  19:21               527 VolcanoApplication.java
    22/10/2008  12:53             1,009 VolcanoRobot.class
    22/10/2008  12:53               347 VolcanoRobot.java
    25/10/2008  14:21               893 Weather.class
    25/10/2008  14:21               672 Weather.java
    25/10/2008  14:20               671 Weather.java~
                  19 File(s)         11,508 bytes
                   2 Dir(s)  33,487,659,008 bytes free
    java TokenTester cmd:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: TokenTester
    Caused by: java.lang.ClassNotFoundException: TokenTester
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  15. #15
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Just to let you know, I've ditched windows now, switched to Linux instead. It doesn't come up with as many pointless errors. Ha ha... Java is working fine here now with the same code that i compiled under windows. However if you could still find a solution to the windows problem it would be much appreciated.
    It never hurts to learn how to solve problems. :P

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    In your copy of the command prompt console, You left off the command you entered to get the error. You only show the error.
    Did you see the command and response I got when I entered a java command in the command prompt above? It showed the command line and the response.

    What command did you enter?
    Or the commandline used a classpath setting that did not include the folder with the missing class.
    Last edited by Norm; 10-29-2008 at 01:37 AM.

  17. #17
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    I just compiled it with javac and then ran it using java TokenTester

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Ok, glad it is now working for you.

  19. #19
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Cheers all that responded, greatly appreciated. Especially Norm and xcallmejudasx

Similar Threads

  1. Replies: 5
    Last Post: 05-14-2008, 01:43 PM
  2. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  3. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •