Results 1 to 5 of 5
- 01-24-2012, 04:19 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
creating array of my class in "main"?
I was reading the Java tutorial on arrays at
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
I can make an array of integers no problem. But when I try to make an array of my own class object, I get either a compile error or a run-time error.
It seems like I'm stuck if I do or if I don't…
Here's the code:
% cat Simple.java
If I comment out the problematic line, then I get a run-time error at the "myList[n].x = 1;" line:Java Code:public class Simple { private class MyType { int x; } public static void main(String[] args) { MyType[] myList; int[] intList; myList = new MyType[10]; intList = new int[10]; for (int n = 0; n < myList.length; n++) { System.out.println("n = "+n); intList[n] = 10; // The compiler does not like the next line. Says "non-static variable this cannot be referenced from a static context" myList[n] = new MyType(); myList[n].x = 1; } } }
% java Simple
n = 0
Exception in thread "main" java.lang.NullPointerException
at Simple.main(Simple.java:20)
…And if I take out the "static" part of main, then the application won't run at all because it can't find the "main" method.
Any suggestions?
Thanks!
- 01-24-2012, 04:55 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: creating array of my class in "main"?
What you have suppose to do on,
Java Code:myList[n].x = 1;
- 01-24-2012, 05:00 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: creating array of my class in "main"?
That makes sense because you aren't putting anything into the array, so myList[n] is null and the myList[n].x part will throw a NullPointerException.If I comment out the problematic line, then I get a run-time error at the "myList[n].x = 1;" line:
That also makes sense because the "static" is what allows the runtime to actually run main() before it ever has an instance of the Simple class. (if it ever does have one.)And if I take out the "static" part of main, then the application won't run at all because it can't find the "main" method.
-----
As for your problem itself, ... Why is MyType an inner class?
If you don't have a reason - and if you don't know what an inner class is, then you can't have a reason - my suggestion is that you take the definition of MyType out of the Simple class. It does not have to go in another file, you can put it in the same file, just outside Simple.
Java Code:public class Foo { // stuff }// <-- Foo finishes here private class Bar { // more stuff }
- 01-24-2012, 05:31 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 01-24-2012, 07:46 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: creating array of my class in "main"?
Let see what OP thinks about that. However, pbrockway2 give a nice comment on his reply.
Similar Threads
-
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
"Cannot find main class" error when creating a .jar!
By davetheant in forum New To JavaReplies: 17Last Post: 11-10-2011, 12:20 PM -
"Could not find the main class" when omitting "-jar"
By b0rt in forum Advanced JavaReplies: 11Last Post: 08-18-2011, 10:54 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 -
"Could not find the main class: comparisonDemo.class. Program will exit."
By ziisrick in forum New To JavaReplies: 6Last Post: 05-18-2010, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks