• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!