Results 1 to 15 of 15
  1. #1
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Exception in thread main

    Heyy Guys,

    Need help here. I wrote a simple java code for arrays as mentioned below

    public class Arraye
    {

    public int size;

    public String name;

    public int getsize()
    {
    return size;
    }

    public void setsize(int s)
    {
    size = s;
    }

    public String getname()
    {
    return name;
    }

    public void setname(String n)
    {
    name =n;
    }

    }
    class TestArraye
    {
    public static void main (String[] args)
    {
    Arraye a = new Arraye();
    a.size =23;

    a.name="chikoo";

    System.out.println(a.getname());
    System.out.println(a.getsize());

    }
    }

    I saved the above code in text file as Arraye.java. When i tried to run this code i got the exception in thread main.

    My doubt is how should i save the java code containing two classes. Should i save if by the name of test class or should i save it by the class name???

    Thank you.

  2. #2
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default

    Nd also i forgot to mention that when am saving the above file by the name of testclass ie by TestArraye.java by making the test class public and removing the public keyword from the actual class, am facing a huge list of compile error.

    Thank you once again.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Remove the 'public' keyword from your 'Arraye' class and add it to your 'TestArraye' class; save the file as 'TestArraye.java'; compile and run your public 'TestArraye' class; that will do for now.

    kind regards,

    Jos

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Hey welcome to the forums!

    First off, please use [code] tags to retain the indentation. Like this:
    Java Code:
    public class Arraye {
    
    	public int size;
    
    	public String name;
    
    	public int getsize()
    	{
    		return size;
    	}
    
    	public void setsize(int s)
    	{
    		size = s;
    	}
    
    	public String getname()
    	{
    		return name;
    	}
    
    	public void setname(String n)
    	{
    		name =n;
    	}
    
    }
    
    class TestArraye {
    	
    	public static void main (String[] args)
    	{
    		Arraye a = new Arraye();
    		a.size =23;
    
    		a.name="chikoo";
    		
    		System.out.println(a.getname());
    		System.out.println(a.getsize());
    
    	}
    }
    Since you didn't copy and paste the exception you're getting, I'm going to walk you through the basic procedure.
    1. TestArraye has to be in its own file; thus, TestArraye.java should contain:
    Java Code:
    public class TestArraye {
    	
    	public static void main (String[] args)
    	{
    		Arraye a = new Arraye();
    		a.size =23;
    
    		a.name="chikoo";
    		
    		System.out.println(a.getname());
    		System.out.println(a.getsize());
    
    	}
    }
    And Arraye.java should contain:
    Java Code:
    public class Arraye {
    
    	public int size;
    
    	public String name;
    
    	public int getsize()
    	{
    		return size;
    	}
    
    	public void setsize(int s)
    	{
    		size = s;
    	}
    
    	public String getname()
    	{
    		return name;
    	}
    
    	public void setname(String n)
    	{
    		name =n;
    	}
    
    }
    2. Make sure both files are in the same directory. Then compile both files.
    This is in CMD
    Java Code:
    [U]SOME DIRECTORY[/U]> cd [U]THE DIRECTORY OF THE SOURCE FILES (.java extension)[/U]
    [U]DIRECTORY[/U]>javac Arraye.java TestArraye.java
    [U]DIRECTORY[/U]>java TestArraye
    If you're still getting an exception/error, please post it.

    Also, read up on encapsulation.
    Last edited by Lil_Aziz1; 06-27-2010 at 06:14 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default

    Hey thx for the quick response. But can't i include both the classes in same file???

    Thank you.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by maya700 View Post
    Hey thx for the quick response. But can't i include both the classes in same file???

    Thank you.
    Yes you can (read my previous reply) but Lil_Aziz1's solution is a much more general solution; try to follow his directions.

    kind regards,

    Jos

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    You would have to make the class TestArraye static.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    You would have to make the class TestArraye static.
    1,$s/static/public/

    kind regards,

    Jos

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh what. I didn't see post #3.
    Wait JosAH, how does that work lol? I just tested it and it does. So removing public from "public class Arraye" makes it protected and adding public to "class TestArraye" makes it public, but Arraye is still isn't in TestArraye's scope. Also, I have no idea what
    Java Code:
    1,$s/static/public/
    means lol

    EDIT: Okay so Arraye doesn't need the scope because TestArraye instantiates it in the main method. Still confused on how this works. When I added private to "class Arraye" it gave me an error. Then, I removed the private from "private class Arraye" and moved the class to the default package and it still works.
    Last edited by Lil_Aziz1; 06-27-2010 at 06:56 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

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

    Default

    The original post creates two class files: Arraye and TestArraye
    Then java TestArraye starts the program:

    Here's my console:
    D:\JavaDevelopment\Testing\ForumQuestions3>java TestArraye
    chikoo
    23

    D:\JavaDevelopment\Testing\ForumQuestions3>
    The question is what did the OP enter when:
    When i tried to run this code

  11. #11
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Heyyy

    hey guys firstly i would like to thank you all for assistance nd also will like to show u some thing. Below i modified the code

    class Arraye
    {
    public int size;

    public String name;

    public int getsize()
    {
    return size;
    }

    public void setsize(int s)
    {
    size = s;
    }

    public String getname()
    {
    return name;
    }


    public void setname(String n)
    {
    name = n;
    }

    }

    public class Testaraye
    {
    public static void main (String[] args)
    {
    Arraye a = new Arraye();

    a.setsize(23);

    a.setname("chikoo");
    System.out.println(a.getname());
    System.out.println(a.getsize());

    }
    }

    nd when i ran it, its throwing the output. what i did was, i made the test class public nd saved it with the testclass name in the same file nd it worked.

    Thanks

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Oh what. I didn't see post #3.
    Wait JosAH, how does that work lol? I just tested it and it does. So removing public from "public class Arraye" makes it protected and adding public to "class TestArraye" makes it public, but Arraye is still isn't in TestArraye's scope. Also, I have no idea what
    Java Code:
    1,$s/static/public/
    means lol
    There can only one public class definition per source file but it can store a bunch of package scope classes. That was my suggestion: make the Arraye class package scope and define the ArrayeTest class the public class (and name the source file correctly). It was a quick and short fix, I didn't want to stand the risk of a bunch of compilation errors and classpath errors as follow ups ;-)

    In vi '1,$s/static/public' means change all occurrences of 'static' to 'public'. It was a little nerdism ;-)

    kind regards,

    Jos

  13. #13
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default

    thanks you.

  14. #14
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    There can only one public class definition per source file but it can store a bunch of package scope classes. That was my suggestion: make the Arraye class package scope and define the ArrayeTest class the public class (and name the source file correctly). It was a quick and short fix, I didn't want to stand the risk of a bunch of compilation errors and classpath errors as follow ups ;-)

    In vi '1,$s/static/public' means change all occurrences of 'static' to 'public'. It was a little nerdism ;-)

    kind regards,

    Jos
    Ooh. I made another a source file called TestArraye2 with a main method that instantiates Arraye and it works! So basically, having class Arraye with package scope in the source file TestArraye is the same thing as having Arraye and TestArraye in difference source file corresponding to their class name (Arraye.java & TestArraye.java) but one avoids the classpath bs. Neat stuff. Thanks :)!
    One more thing, what does VI stand for? VIsual basic? Thought that was VB.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    One more thing, what does VI stand for? VIsual basic? Thought that was VB.
    Vi is the good old Unix editor which originally was a visual interface (hence the name) on top of 'ex' the real editor which in turn was a superset of the old 'ed' editor. Nowadays we have 'vim' (Vi IMproved) written by another Dutchman Bram Moolenaar; you'll find it on each and every Linux machine. There are also versions available for MS Windows (for free!) I guess the http address is http:/www.vim.org. Give it a try, it's a tremendous editor once you get used to it (some people never will ;-)

    kind regards,

    Jos

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 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
  •