• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
no mistakes in Eclipse in the synthax highlighting edit view but the program gives the mistake above

ps. there is a method to fast TEST the book codes in Eclipse without assigning a new Java Project and then new Class, then cancel? I mean there are online sites that allow to copy paste codes without requiring a java class

 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It actually says more than just what you listed...it tells you the exact line where the problem is:




My question to you would be what exact value to you expect to be assigned to j on line 9?
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got NPE, because your i variable is null.
Be careful. Default value fot int type is 0. Default value fot Integer type (as for every non-primitive type) is null.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declare i as an Integer but don't initialise it. Since it's a reference type, its default value is null.

Then you assign that to an int. That's a primitive type, and so cannot have the value null. Guess what happens?

Eclipse didn't show any errors because there are no errors in the syntax. This will compile fine. You're getting a run-time error, which depends on the specific value of i when the go() method is run. If it was set to a non-null value before calling go() it would have run without error.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:It actually says more than just what you listed...it tells you the exact line where the problem is:




My question to you would be what exact value to you expect to be assigned to j on line 9?


Thank you Fred to give me the opportunity to do some brain dumbell. really Appreciated

so... integer i is an instance variable assigned to the beginning of the class, now I know that instance variables differently by local variables( inside the methods have e default value, so an int is 0 also if i do not write int i =0; a boolean is false, a float is 0.0. so I would suppose that integer i has a value of zero or null (could be an object more than a primitive int) while int j; will have allocated a 0 as is an int.
when the go method will be called the variable i will be assigned to j so both of them should become null null or zero zero, and this is the output that i would i would expect in a perfect JVM world!
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear alll three of you
I have just read your comments, thanks

the problem is that null won't be printed. I think I understand why. the reason is that a null value is a pointer to nothing not a variable. so the compiler says : ehy dude I cannot assign null to zero because null is not a value.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:so the compiler says : ehy dude I cannot assign null to zero because null is not a value.

Nitpick time!
1. It's JVM that tells you that. Not the compiler. Compiler doesn't mind as the syntax is correct.
2. In your example you were trying to assign null to int variable, not null to zero.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Giovanni Montano wrote:so the compiler says : ehy dude I cannot assign null to zero because null is not a value.

Nitpick time!
1. It's JVM that tells you that. Not the compiler. Compiler doesn't mind as the syntax is correct.
2. In your example you were trying to assign null to int variable, not null to zero.


thank youd dear ranch friend, you gained 1 point for your nitpicky finicky approach, you are seriously contributing to my ascent as future Java guru. really appreciated.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:thank youd dear ranch friend, you gained 1 point for your nitpicky finicky approach, you are seriously contributing to my ascent as future Java guru. really appreciated.

Is it some kind of sarcasm or not? I can't tell
What I can tell is that in programming (even at beginner level) being precise is VERY important.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Giovanni Montano wrote:thank youd dear ranch friend, you gained 1 point for your nitpicky finicky approach, you are seriously contributing to my ascent as future Java guru. really appreciated.

Is it some kind of sarcasm or not? I can't tell
What I can tell is that in programming (even at beginner level) being precise is VERY important.

HI Pawel,
my excuses, no sarcasm at all, I just love that people help me like you did it. I love to be challenged and from this post I learned a lot. JVM vs compiler, zero vs int, null error, so I joke because smart people do it, I just discovered my love for programming after 40 years of attempts to understand what i like to do in life outside playing mediocre chess games, picking up girls and pretending I am an accountant
thank you again Pawel.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:so... integer i is an instance variable

My turn to be nit-picky.

There is no such thing as an "integer" in java. You have the "int" type which are primitives, or you have "Integer" type, which are objects.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omigod! Nitpick war!
fred rosenberger wrote:There is no such thing as an "integer" in java. You have "int" types (primitives), or you have "Integers", which are objects.

No, you don't have int types! You have int type! There is only one int type. Not many ;).
Not talking about integer types ;)
Your turn ;).

EDIT: Do you have to have moderator rights to edit your own post? I don't think so!
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok clear class Object rules the world;)

Integer is an object or better if I will won the golden nitpick context saying integer i; is not an object but a reference variable to an object;)( I think I am becoming a guru )
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Omigod! Nitpick war!
fred rosenberger wrote:There is no such thing as an "integer" in java. You have "int" types (primitives), or you have "Integers", which are objects.

No, you don't have int types! You have int type! There is only one int type. Not many ;).
Not talking about integer types ;)
Your turn ;).

I don't know what you are talking about.

[abusing moderator power to edit my previous post is one of the perks!!!]
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He is saying int is a (primitive)type so is singular, while you typed wrong ;) int types
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:[abusing moderator power to edit my previous post is one of the perks!!!]

See my previous post ;)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the Exception is thrown when you try to un‑box the null.

[edit]Add this link to the Java Language Specification. Look at the last bullet point under §5.1.8.[/edit]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:integer i; is not an object but a reference variable to an object

My turn
If you are carrying on the conversation in this post, then it is neither. As has already been pointed out, the wrapper class for an int is Integer.

Of course, it could be the case that you have defined your own class called integer, inwhich case i would be a reference variable as you say. However, I would still nitpick by saying that it is accepted custom to start class names with a capital letter and it is also a bad idea to have your own class with a name that is very similar to the name of a class in the standard API.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Do you have to have moderator rights to edit your own post? I don't think so!

Anyone can edit their own posts for <some period of time>. Moderators are not restricted by this limitation. So there will come a point where you can't edit yours any more, but I could still edit mine. Although I try not to except for grammar/gross errors/having a little fun now and then.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joanne

Just to understand OO
Thank you but in a certain way can I say integer i is reference variable regarding the following class within java.util. Integer?thank you
866 public Integer(String s) throws NumberFormatException {
867 this.value = parseInt(s, 10);
868 }


In
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:Thank you but in a certain way can I say integer i is reference variable...

Not only can you say it is. It IS.

The problem you're running into actually has nothing to do with any of the above; it has to do with auto-boxing (or actually, in this case, unboxing). In a perfect world:
j = i;
would have given you a compiler error - and before version 5, it would have, because j is NOT the same type as i.

Since version 5, the compiler does allow to to assign primitives to wrapper objects (and vice-versa), but the one thing it can't cater for is when you try to assign a null to a primitive - which is exactly what you're doing.

Before version 5, you would have had to write:
j = i.intValue();
and the compiler would have enforced it.

Now: anything goes. (sigh)

Winston
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Giovanni Montano wrote:Thank you but in a certain way can I say integer i is reference variable...

Not only can you say it is. It IS.
(mega CUT )
Winston

thank you nerds i think i love you already!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
Before version 5, you would have had to write:
j = i.intValue();
and the compiler would have enforced it.
. . .
… and you would still have suffered a NullPointerException.

The Java Language Specification talks about integral types.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The Java Language Specification talks about integral types.

This is what you get when you declare a nitpick war
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:… and you would still have suffered a NullPointerException.

True, but then it would be entirely your own fault. If you go to all the business of typing j = i.intValue() without thinking about what might happen when i is null, you deserve everything you get.

Unfortunately, autoboxing is easy to forget.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote: . . .
This is what you get when you declare a nitpick war
Yes, I know, but how can you tell people to shut up about the nitpicking?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . how can you tell people to shut up about the nitpicking?
Particularly if the nitpick war is more fun than the rest of the thread
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!