Results 1 to 5 of 5
  1. #1
    sbooby2365 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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
    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;
                }
      }
    }
    If I comment out the problematic line, then I get a run-time error at the "myList[n].x = 1;" line:

    % 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!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: creating array of my class in "main"?

    What you have suppose to do on,

    Java Code:
    myList[n].x = 1;

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: creating array of my class in "main"?

    If I comment out the problematic line, then I get a run-time error at the "myList[n].x = 1;" line:
    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.

    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.
    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.)

    -----

    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
    }

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: creating array of my class in "main"?

    Quote Originally Posted by Eranga View Post
    What you have suppose to do on,

    Java Code:
    myList[n].x = 1;
    Maybe to assign value to field of MyType object?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. "Cannot find main class" error when creating a .jar!
    By davetheant in forum New To Java
    Replies: 17
    Last Post: 11-10-2011, 12:20 PM
  3. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  4. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  5. Replies: 6
    Last Post: 05-18-2010, 05:11 PM

Posting Permissions

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