• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

[error] Exception in thread "main" java.lang.NullPointerException

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks a bit odd to me because "I thought it was working yesterday".



Screendump at http://cid-b712073b3513eb8e.skydrive.live.com/self.aspx/.Public/Eclipse-screendump.png

Environment notes:
Eclipse Build id: M20090211-1700 3.4.2
Java 1.6

 
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you work out from the stack trace? The screen dump is too small to read, I am afraid.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What did you work out from the stack trace? The screen dump is too small to read, I am afraid.



It obviously depends on what kind of monitor you are using, but if you click within the image it expands in a manner which is readable (to me). Then you can zoom in.

What I find hard to understand is why my manager[0] object has a value of NULL after calling the Employee.compareTo() method (Employee is a superclass of Manager).

Stacktrace:
[i]java.lang.NullPointerException
at homenetwork.bkr.training.Test.main(Test.java:43)
which is this line

Exception in thread "main" java.lang.NullPointerException
at homenetwork.bkr.training.Test.main(Test.java:51)[/i]
which is this line



NOTE: Btw the function Copy to Clipboard on this forum does not work here. I have tried with Google Chrome and Mozilla FF, and, although the message confirming that the text (code) was copied to the clipboard, I can't paste anything to say Notepad.



 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What I find hard to understand is why my manager[0] object has a value of NULL after calling the Employee.compareTo() method (Employee is a superclass of Manager).



Manager[0] does *not* have a value of null. It is another array element that has the value of null. Remember that the for loop cycles through all manager elements.

If you look at the code carefully, you should easily see which element has a value of null -- and hence, causing the exception.




And BTW...

It obviously depends on what kind of monitor you are using, but if you click within the image it expands in a manner which is readable (to me). Then you can zoom in.



Nope. It does not depend on the monitor. I saved the graphic and zoomed in with my graphic editor. There is no detail there. It just becomes blurry and unreadable.

Henry
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

What I find hard to understand is why my manager[0] object has a value of NULL after calling the Employee.compareTo() method (Employee is a superclass of Manager).



Manager[0] does *not* have a value of null. It is another array element that has the value of null. Remember that the for loop cycles through all manager elements.

If you look at the code carefully, you should easily see which element has a value of null -- and hence, causing the exception.

What do you mean? I tend to ask after I look and I don't understand; most people do that


And BTW...

It obviously depends on what kind of monitor you are using, but if you click within the image it expands in a manner which is readable (to me). Then you can zoom in.



Um.. I'm also having a problem with the Manager.clone() method, because it seems to be setting the previous copy to a null value:


calls


Output:
New clone: null
Copy of: Will White
}
[


Nope. It does not depend on the monitor. I saved the graphic and zoomed in with my graphic editor. There is no detail there. It just becomes blurry and unreadable.

I asked skydrive about it, maybe you would post some environment notes so that when Microsoft decide to read my email they will know about it (er..hopefully)

Henry

 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What do you mean? I tend to ask after I look and I don't understand; most people do that




You have a manager array. You forgot to set one element in that array -- hence, one of the elements is null. And you iterate through the whole array -- hence, you get a null pointer exception, when your code dereferences that element.

Henry
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

What do you mean? I tend to ask after I look and I don't understand; most people do that




You have a manager array. You forgot to set one element in that array -- hence, one of the elements is null. And you iterate through the whole array -- hence, you get a null pointer exception, when your code dereferences that element.

Henry



Indeed, thanks Reminds me, I still have to find a solution for a "dynamically growing array".

However I am still getting manager[0] being set to a null value, and, I don't really understand why I have to create all the objects in the array so that the managers[1].compareTo(managers[2] == 0 works:



Also my Manager.clone() method doesn't seem to work.

public Manager clone (Manager anotherManager)
{
return anotherManager;
}

 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon --

1) You're setting three of the four elements in the array, then sorting the array. Apparently the empty (null) element is getting sorted to managers[0], hence the NullPointerException. You have to set all the elements of the array if you want them all to be predictably non-null after a sort.

2) Yeah, we know about the "copy to clipboard." It turns out that the only way to code this involves embedding a tiny flash program (!) and even so, smart browsers like Firefox block the trick for security reasons. Instead use "view plain" and then copy the text from the small window that pops up.

3) Your screenshot worked fine for me.

 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Jon --

1) You're setting three of the four elements in the array, then sorting the array. Apparently the empty (null) element is getting sorted to managers[0], hence the NullPointerException. You have to set all the elements of the array if you want them all to be predictably non-null after a sort.

2) Yeah, we know about the "copy to clipboard." It turns out that the only way to code this involves embedding a tiny flash program (!) and even so, smart browsers like Firefox block the trick for security reasons. Instead use "view plain" and then copy the text from the small window that pops up.

3) Your screenshot worked fine for me.



Thanks. So Arrays.sort won't sort a null value
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Camilleri wrote:

Thanks. So Arrays.sort won't sort a null value



Well, if you're lucky, sort() will never call compareTo() on a null value; it might work out that it's only called on non-null values, passing the null as an argument. Then it's up to your implementation of compareTo() -- if it always returns -1, for example, if the argument is null (which it will if the first line checks the argument with "instanceof", which always returns false for a null argument) then it's actually possible -- as appears to be happening here -- to sort an array with nulls in it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic