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

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

  1. #1
    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

    I'm also getting a runtime null pointer exception with this version when MyClass.set(int) is called, and I suspect it has to do with the creation of the array elements.
    public class Main {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];
        System.out.println(String.format("array is %s elements long", mcArray.length));
     
        // attempt to set array elements
        for(MyClass thisOne : mcArray){
          thisOne.set(2);
        }
      } // main
    } // Main
    Last edited by AngleWyrm; July 9th, 2022 at 08:19 PM.

  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

    Moved to own thread.

    getting a runtime null pointer exception
    What variable has the null value?
    Where in the code is that variable given a value?

    has to do with the creation of the array elements.
    Declaring an array does not give any value to the array's elements. Each element of the array needs to be assigned a value before it is used.
      MyObject[] anArray = new MyObject[2];   // creates an array with 2 elements containing null
      anArray[0] = new MyObject();                 // assign a value to first element of array
    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

    Quote Originally Posted by Norm View Post
    What variable has the null value?
    Where in the code is that variable given a value?
    Quote Originally Posted by AngleWyrm View Post
    I'm also getting a runtime null pointer exception with this version when MyClass.set(int) is called
    Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:20)

    Is there a line numbering feature for this forum's code boxes?

  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

    Is there a line numbering feature for this forum's code boxes?
    I don't think there is.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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

    I notice this behavior of array of myclass objects doesn't seem identical to the behavior of, say an array of booleans
      boolean[] destinationBag;
     
      // load a destination bag with second bag's contents
      destinationBag = new boolean[secondBag.length + 1];
      for( int index = 0; index < secondBag.length; index++){
        destinationBag[index] = secondBag[index];
      }

      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;
        mcArray = new MyClass[10]; // space allocated, but elements still 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(); // uncomment to create array element
          thisOne.set(2);
        }
      } // main
    } // Main
    Last edited by AngleWyrm; July 9th, 2022 at 09:33 PM.

  6. #6
    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

    array of myclass objects doesn't seem identical to the behavior of, say an array of booleans
    Yes, primitives have a default value. An array of a class does not and is initially null.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    AngleWyrm (July 10th, 2022)

  8. #7
    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

    Is it possible to override this behavior through the use of a default constructor?

  9. #8
    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

    override this behavior through the use of a default constructor
    I don't know that arrays have any constructors.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    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

    import java.util.Arrays;
     
    public class Main {
      public static void main(String[] args){
     
        // simple class 
        class MyClass{
          Integer x = 100;
          // initialization constructor
          public MyClass(Integer starting_x){
            this.x = starting_x;
          }
          public void set(Integer new_x){
            this.x = new_x;
          }
          // string representation
          public String toString(){
            return "x:" + x.toString();
          }
        } // MyClass
     
        // create array
        MyClass[] mcArray;
        mcArray = new MyClass[10]; // space allocated, but elements still null
        System.out.println(
          String.format("array is %s elements long", mcArray.length)
        );
     
        // populate array
        for(Integer index = 0; index < mcArray.length; index++){
          mcArray[index] = new MyClass( 10 * (index+1) ); 
        }
        mcArray[4].set(5000);
     
        System.out.println(Arrays.toString(mcArray));
      } // main
    } // Main

    typical run output
    array is 10 elements long
    [x:10, x:20, x:30, x:40, x:5000, x:60, x:70, x:80, x:90, x:100]
    Last edited by AngleWyrm; July 10th, 2022 at 08:46 PM.

Similar Threads

  1. Please help me to find out the error in the following code
    By StudentJava123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 10th, 2022, 05:54 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