Results 1 to 15 of 15
Thread: Exception in thread main
- 06-27-2010, 05:43 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 45
- Rep Power
- 0
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.
- 06-27-2010, 05:47 PM #2
Member
- Join Date
- Jun 2010
- Posts
- 45
- Rep Power
- 0
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.
- 06-27-2010, 06:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 06-27-2010, 06:08 PM #4
Hey welcome to the forums!
First off, please use [code] tags to retain the indentation. Like this:
Since you didn't copy and paste the exception you're getting, I'm going to walk you through the basic procedure.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()); } }
1. TestArraye has to be in its own file; thus, TestArraye.java should contain:
And Arraye.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()); } }
2. Make sure both files are in the same directory. Then compile both files.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; } }
This is in CMD
If you're still getting an exception/error, please post it.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
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)
- 06-27-2010, 06:18 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 45
- Rep Power
- 0
Hey thx for the quick response. But can't i include both the classes in same file???
Thank you.
- 06-27-2010, 06:28 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 06-27-2010, 06:28 PM #7
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)
- 06-27-2010, 06:29 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 06-27-2010, 06:49 PM #9
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 whatmeans lolJava Code:1,$s/static/public/
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)
- 06-27-2010, 07:18 PM #10
The original post creates two class files: Arraye and TestArraye
Then java TestArraye starts the program:
Here's my console:
The question is what did the OP enter when:D:\JavaDevelopment\Testing\ForumQuestions3>java TestArraye
chikoo
23
D:\JavaDevelopment\Testing\ForumQuestions3>When i tried to run this code
- 06-27-2010, 07:23 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 45
- Rep Power
- 0
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
- 06-27-2010, 07:29 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 06-27-2010, 07:35 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 45
- Rep Power
- 0
thanks you.
- 06-27-2010, 07:52 PM #14
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)
- 06-27-2010, 07:59 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks