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 4 of 4

Thread: Please help me to find out the error in the following code

  1. #1
    Junior Member
    Join Date
    Jul 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me to find out the error in the following code

    public class ObjectArray {

    public static void main(String[] args) {
    Account obj[] = new Account[10];
    obj[0].setData(1,2);
    obj[1].setData(3, 4);
    System.out.println("For Array Element 0:");
    obj[0].showData();
    System.out.println("For Array Element 1:");
    obj[1].showData();
    }


    public class Account{
    int a;
    int b;
    public void setData(int c,int d){
    a=c;
    b=d;
    }
    public void showData(){
    System.out.println("Value of a ="+a);
    System.out.println("Value of b ="+b);
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help me to find out the error in the following code

    Why do you think there is an error?
    Do you get some messages when you try to compile and execute the code?
    Please copy the full text of any messages and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Please help me to find out the error in the following code

    The array created in the original code needs a bit where the array elements are created.

    Here's a version that doesn't throw a run-time exception.
    public class Main {
      public static void main(String[] args){
     
        // class
        class MyClass{
          int x;
          public int get(){
            return this.x;
          };
          public void set(int newX){
            this.x = newX;
          }
        }
        // create array
        MyClass[] mcArray = new MyClass[10]; // size allocated, but elements are null
        System.out.println(String.format("array is %s elements long", mcArray.length));
     
        // attempt to set array elements
        for(MyClass thisOne : mcArray){
          thisOne = new MyClass(); // create new array element where null element was
          thisOne.set(2);
        }
      } // main
    } // Main

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help me to find out the error in the following code

    needs a bit where the array elements are created
    Note that code does not set the values of the elements of the array. To see what is in the array, add this print statement after the loop:
       System.out.println(java.util.Arrays.toString(mcArray));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: May 8th, 2014, 06:25 AM
  2. [SOLVED] Can't find solution for error in my code..
    By adi2609 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2013, 11:10 AM
  3. Cannot find symbol error in my java code
    By haliza hadi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 07:20 PM
  4. Cannot find Error in Beginner code!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2012, 08:44 PM
  5. Code won't compile, but can't find the error....
    By RockDoc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:09 AM