Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with Arrays

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Problem with Arrays

    Hello,
    Okay I'm stuck here, and have been for a while I've searched everything I can and still can't seem to find out what is going on. Alright I have an Object Called Aircraft. The Object Airplane has the following Fields Aircraft_Id (String), Aircraft_Type (String), Index (BigDecimal), BOW (Int). I have two different Constructors, has nothing passed to it and sets all the fields to an default Value, and a second one that requires ALL fields be supplied.

    When I try to set a Field (i.e. Airplane.Aircraft_Id = "Some Aircraft Id") everything seems to work just great, I can set it, and later retrive it.
    Unforuntatly I don't find see having 100 differenct aircraft Instances as very efficent so I wanted to create an array of Airplanes.

    static public Airplane Aircraft[] = new Airplane[100];

    Everything Seems to work fine until I try to set a field

    Aircraft[1].Aircraft_Id = "Some Aircraft Id";

    This causes my program to crash.

    A Side note this is to be run on Android however, this appears to be a failure in my understanding of basic Java is the reason why I'm here and not an an android developers forum.

    Thank You for your Help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Arrays

    This causes my program to crash.
    It helps to define 'crash'...eg the exception you receive. Did you instantiate the members of the array? You can create an array of Objects, but unless you actually instantiate these they will by default be null

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Arrays

    Quote Originally Posted by copeg View Post
    It helps to define 'crash'...eg the exception you receive. Did you instantiate the members of the array? You can create an array of Objects, but unless you actually instantiate these they will by default be null
    I would define Crash as the Program ends.

    I was wondering if you had to create each member of the array or not, but i would assume that they would have been all created simply throught the act of creating the array. How does one go about creating an istance of the object with in the array?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Arrays

    Did you see the console? Do you know what an exception is?
    Yes you have to instantiate members of the array. See the following, which defines an array with 1 element not instantiated, and which will throw a NullPointerException because of this
     
    String[] myString = new String[2];
    myString[0] = "My String";//instantiate the first element
    if ( myString[1].equals(myString[1] ) {
       System.out.println("The above line will throw a NullPointerException");
    }

  5. The Following User Says Thank You to copeg For This Useful Post:

    dashpilot (March 6th, 2012)

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Arrays

    Okay I got it figured out. As far as I can tell there isn't a console for Android Apps, or at least not one that tells you anything.

Similar Threads

  1. Problem with arrays and custom objects
    By MonkeyBeast in forum Collections and Generics
    Replies: 1
    Last Post: January 31st, 2012, 01:50 PM
  2. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  3. 3 Arrays
    By D3158 in forum Collections and Generics
    Replies: 27
    Last Post: June 16th, 2011, 11:10 AM
  4. Problem with arrays
    By coorscollector in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 03:50 AM
  5. 2 D arrays problem
    By Pulse_Irl in forum Collections and Generics
    Replies: 2
    Last Post: March 5th, 2010, 04:49 PM