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

Thread: Null Pointer (a curious case)

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Null Pointer (a curious case)

    I get a null pointer at:

    n[Array.getLength(n)-1] = cur;

    not an arrayIndexOutOfBounds, but a null pointer? I didn't think you get get a null pointer on assignment, because after all aren't you supposed to be able to assign any object a null?

    EDIT: n is a non primitive type. It is actually an array of objects.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Null Pointer (a curious case)

    Is n itself null?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    It shouldn't be:

    	n=utilities.increment(n);
    			for(Tile cur:t.nodes){
    				if(cur!=null){
    				n[Array.getLength(n)-1] = cur;

    	public static Tile[] increment(Tile[] k){
    		int i = 0;
    		int len = k==null ? 1:Array.getLength(k)+1;
    		Tile[] tmp = new Tile[len];
    		if(len==1)return tmp;
    		for(Tile kk: k){
    			tmp[i] = kk;
    			++i;
    					}
    					return tmp;
    								          }

    I actually preform a null check on cur for operations that occur later.


    Additional info:

    n is a local variable, but was passed as a parameter. So to my knowledge as soon as I reassigned n it lost its connection to the passed array.

    Post Script: I do understand your question though, because if I am being pragmatic I can concede that the system doesn't lie. So the only option here is that n is null? I guess my question for this thread is: could there be any other way I would get a null pointer there besides the fact that n is null?
    Last edited by KAJLogic; April 8th, 2014 at 09:47 AM.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    Is the type for n explicitly an array, or is it just an object?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    What do you mean by explicitly an array?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Null Pointer (a curious case)

    Can you post an SSCCE that demonstrates exactly what you're doing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    KAJLogic (April 8th, 2014)

  8. #7
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    May I ask first, is there any other way I could get a null pointer there? Or, do you need more of the code to know for sure? In my head I keep getting this nagging urge to just stop myself and realize that an assignment cannot get a null pointer; the only way is if the array is null (). Is this not true? (assuming n is a non-primitive type)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Null Pointer (a curious case)

    Another way is that Array isn't the class but a local variable that is null.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    There is a level of incursion, but I believe that as soon as I reassign n it is no longer referencing the argument. Norm, do you mean a local variable that wasn't initialized?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Null Pointer (a curious case)

    variable that wasn't initialized
    That is the most common cause of a NPE.
    If you don't understand my answer, don't ignore it, ask a question.

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

    KAJLogic (April 8th, 2014)

  13. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    Quote Originally Posted by KAJLogic View Post
    What do you mean by explicitly an array?
    Like, is it declared as:
    Tile[] n;
    or it is declared as:
    Object n;

    I ask because if it is the first one (explicitly an array), you could get the length by:
    n.length;
    Instead of:
    Array.getLength(n);

    NOTE: notice no parentheses at the end of n.length. It is a variable reference, not a method call.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  14. #12
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    Yes aussie your right I guess I never really reviewed the array API very much. In any case I appreciate your help. I will delve into this with your suggestions.

  15. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    I suggested this because if you use n.length; and still get the null pointer, it means two things:
    1. the problem is independent from the Array class
    2. n is null
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  16. #14
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    What are you suggesting that the Array class is inaccurate or flawed?

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Null Pointer (a curious case)

    Without seeing a complete (small) program that compiles, executes and shows the problem, most of this discussion is pretty useless.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Null Pointer (a curious case)

    Quote Originally Posted by KAJLogic View Post
    What are you suggesting that the Array class is inaccurate or flawed?
    No.

    Quote Originally Posted by Norm View Post
    Without seeing a complete (small) program that compiles, executes and shows the problem, most of this discussion is pretty useless.
    Yes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #17
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    No, but perhaps that the getLength() method does something which, due to the way your array is set up or something, unexpectedly causes a null pointer to be thrown. The Array.getLength() method is a native method, so I don't have a clue what it actually does.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  20. #18
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    Aussie using the length variable produces the same error.

     
     
     
    								        			        }
    Last edited by KAJLogic; April 8th, 2014 at 01:38 PM.

  21. #19
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    Well that means that n is null.
    n is set by the utilities.increment() method. Unfortunately, it doesn't appear that the increment() method can return null. Are you 100% sure the line you are getting the null pointer at is correct?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  22. #20
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer (a curious case)

    Yeah at the top of the exception it cites that line then it cites a stream larger than my command prompt buffers capabilities here:

    	nO = t.getOptimalNodes(n,utilities.invert(a));
    Last edited by KAJLogic; April 8th, 2014 at 01:04 PM.

  23. #21
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Null Pointer (a curious case)

    Can you provide the full stack trace?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  24. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Null Pointer (a curious case)

    This will be trivial to debug as soon as you provide the SSCCE. Before that, this is all just guesswork.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Null Pointer Excpetion help......
    By nutan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2013, 10:24 PM
  2. Help With Null Pointer Exception
    By kendraheartt in forum Exceptions
    Replies: 1
    Last Post: August 15th, 2012, 10:36 PM
  3. null pointer exception
    By Ramzi89 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 15th, 2012, 01:57 PM
  4. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  5. Reflection - null pointer
    By bgalakazam in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2010, 06:55 PM