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

Thread: NullPointerException

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default NullPointerException

    Hello.
    Why is the NullPointerException generated in the following code snippet at run-time?
      		class Info{			
    			public String name;
    			public String version;
    			public String arch;
    			double CPUSpeed;
    		};
    		Info info =  new Info();
    		Info[] queue = new Info[100];
     
    		int pos = 0;
     
    				queue[pos].name = System.getProperty("os.name");
    				System.out.println( queue[pos].name);
    (the last two lines are problematic)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: NullPointerException

    Define 'problematic' - in Java terms. Post your errors in their entirety, copied from exactly as they appear at your end.

    Java statements apart from variable declarations and initializations must occur within a method. I don't see a method in what you've posted. And there are other syntactic problems with what you've posted.

    What are you trying to do?

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: NullPointerException

    Info[] queue = new Info[100]; // Array that CAN hold 100 Info references
    int pos = 0;
    queue[pos].name = System.getProperty("os.name"); // First slot of the array was never filled with an Info reference

  4. #4
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Quote Originally Posted by PhHein View Post
    Info[] queue = new Info[100]; // Array that CAN hold 100 Info references
    int pos = 0;
    queue[pos].name = System.getProperty("os.name"); // First slot of the array was never filled with an Info reference
    What shall I do?

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: NullPointerException

    Just a bit of info in-case your are unsure of what the array-pointer thing is

    There is a very close connection between pointers and arrays. In fact they
    are more or less one and the same thing. When you declare an array as:

    int[]a;
    You in fact declaring a pointer a to the first element in the array. That is, a is exactly the same as &a[0].
    The only difference between a and a pointer variable is that the array name is a constant pointer
    - you cannot change the location it points at. When you write an expression such as a[i]
    this is converted into a pointer expression that gives the value of the appropriate element.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. #6
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Quote Originally Posted by Ada Lovelace View Post
    Just a bit of info in-case your are unsure of what the array-pointer thing is

    There is a very close connection between pointers and arrays. In fact they
    are more or less one and the same thing. When you declare an array as:



    You in fact declaring a pointer a to the first element in the array. That is, a is exactly the same as &a[0].
    The only difference between a and a pointer variable is that the array name is a constant pointer
    - you cannot change the location it points at. When you write an expression such as a[i]
    this is converted into a pointer expression that gives the value of the appropriate element.

    Wishes Ada xx
    But I wrote
    Info[] queue = new Info[100]
    madam.
    Doesn't that create an anonymous pointer and assign queue to refer to it?

    Do I instead write Info queue[] = new Info[100]?

  7. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: NullPointerException

    Do I instead write Info queue[] = new Info[100]?
    no, because arrays in Java are not declared like that. I was just pointing out to
    you what the difference between a pointer and an array was under the hood. The syntax
    I showed was using the C language - which is not a managed language like Java is.

    In this stance, because Java handles all memory allocation for you - it might look like this:

    info[]queue = new info[100];

    new will create an instance of info and assign the result to queue.
    So, queue - because it has the instance of info - would act as a pointer
    to the first element of the array info.

    *queue->info[0] // this is suggested - not correct syntax

    Please also note that what I am trying to explain is how the memory is allocated
    using arrays and pointers. This is not an answer to your original question, but
    instead to give you a bit of understanding on how things work under the hood.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  8. #8
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Thanks
    I just needed to new each element separately

Similar Threads

  1. [SOLVED] NullPointerException
    By LuisEPB in forum AWT / Java Swing
    Replies: 8
    Last Post: November 25th, 2013, 05:48 AM
  2. NullPointerException @@
    By bernard1606 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2013, 09:03 PM
  3. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  4. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  5. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM