• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nullpointer exception

 
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

i have a problem with the program i'm writing.
This program is done to help memorize how String methods work. Basically, from command line,
it asks the user to insert a word and then it asks the result of a method (for instance indexOf) on
the word inserted.

So in the program I have an abstract class

Then i have two different derived class. I thought that having declared and initialized firstInput
in the abstract class, the value of firstInput would be fixed.
BUT when i call the second derived class, i have to insert again a word.

So i thought that maybe if i moved the firstInput creation (the line of code written above) in the
working class, and then create two instance of the derived class and passing the value of
firstInput as parameter of their constructor i'd solve the problem. But, i get the nullpointer ex.
To be precise: the program compile, run and asks to insert a word. After i insert it, it crash
because the nullpointer.
What i don't understand is that in theory, if i'm not wrong, it should work. The value of
firstInput is not null as soon as i write it in command line, right? Why it goes as nullpointer?

Any suggestions?
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print the stack trace and you'll see the source file/class name and line number where the exception was thrown.
 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, Ivan.
I run the program from command line, so it already tells me where the exception is thrown.
And it's thrown by firstInput.
My point is that i don't understand why.
firstInput is initialized the moment the user insert some text. And the exception is thrown
exactly after the user insert a word.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most common cause of a NullPointerException is trying to call a method on a variable that is null.

The stack trace tells you exactly in which line of your code the exception happens. So, concentrate on that line, and look what might be null there.

If you want more specific help, then please past the exact error message and the exact line of code where the exception happens. (The more precise you are with this, the easier it is to find the cause).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Derek wrote:What i don't understand is that in theory, if i'm not wrong, it should work. The value of
firstInput is not null as soon as i write it in command line, right? Why it goes as nullpointer?


A couple of tips for you, since I think most of the basics have been covered by others:

1. Don't go into a debugging session with the idea that "it should work". You're far better off assuming that your code is WRONG (because it almost certainly is); you just need to find out where.

2. Most NullPointerException's are preventable, but it takes a bit of 'paranoia' on your part - oddly enough, not a bad thing in programming - the most basic of which is to check ALL reference values passed to methods or constructors for null.

3. (Following on from 2) Fail fast and fail LOUD - that is: catch problems as early as possible, and fail in a way that you can't possibly ignore. Most problems to do with NullPointerException arise from the fact that it is an effect, not a cause; so if you don't catch it quickly, the error can crop up a long way from where it was actually caused.

HIH

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but it takes a bit of 'paranoia' on your part - oddly enough, not a bad thing in programming



Paranoia? It sounds terrible!
Let us word it as "defensive programming" and it sounds much better.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:Let us word it as "defensive programming" and it sounds much better.


Hey, just because you're paranoid doesn't mean NPEs ain't out to getcha.

Winston
 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Winston and Jesper, thank you for your kind reply.

@Winston you are right (the code IS wrong) but it's ironically funny to presume to have done things right, when
you know it's not so.

 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, after long and troubled meditation, hope someone can help me.

Here is the workflow of the version that works.

This is the starting class (with main)(excerpt)

This is the base class (IndexOfVariants is the child class). It's a simplified version.


Then there is IndexOfVariants which does the work.
In this way, everything is all right.

I wanted to change things. In this version the creation of UserInput and firstInput
are made in the working class. I added a constructor to GeneralMethods and
passed the value of firstInput in the constructor.
working class(with main) (excerpt)


In GeneralMethods the value of firstInput "in my calculations" should be passed to Parameters listOfParam.
GeneralMethods class (base class of IndexOfVariants):



But i get the nullpointer ex. I've understood that the problem is firstInput in GeneralMethods.
If i give a value to it, i dont get the error anymore. But i thought that modifing the value
through the constructor of GeneralMethods the value of firstInput should be updated. But
it's not so.

What i don't understand is why firstInput doesn't take the value inserted in command line?
I mean in the first example, it's all right, it works. But in the second example it doesn't.
What's the difference?
For what i have understood about inheriting and OOP, it should be the same... but it's not!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Derek wrote:


The bit that's ringing a large alarm bell for me is using something - firstInput - to initialise listOfParam when firstInput isn't set until the constructor is run. I'm pretty sure that the initialisers are run before the constructor - regardless of whether they are declared before or after the constructor - and that's probably what is causing the error.

If the order things are initialised in matters, do it all in the constructor:

 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Matthew,
thank you for your kind reply.

In fact your intuition has brought something interesting, also if not the solution.
I'll explain.

I've changed GeneralMethods as you suggested. But the error was there. So i've put
a println in the constructor, run the program, and it prints the value of firstInput!
So firstInput cannot be null. So i looked with more attention to the nullpointer indications.
Here is the modified GeneralMethods class:
i've inserted also line 06 and 07 to check the value of firstInput:


iMac-di-Alex:class Sarjo$ java cam.pippo.ExerciseWithString
Please insert a word, any character allowed asefse <-- word inserted by me through command line.
asefse <-- this is the first println in the GeneralMethods Constructor line 4
e <-- this is the variable be (line 6 and 7).
Exception in thread "main" java.lang.NullPointerException
at cam.pippo.Parameters.setFirstCharParameter(Parameters.java:19)
at cam.pippo.Parameters.getFirstCharParameter(Parameters.java:23)
at cam.pippo.methodIndexOf.IndexOfVariants.<init>(IndexOfVariants.java:11)
at cam.pippo.ExerciseWithString.go(ExerciseWithString.java:26)
at cam.pippo.ExerciseWithString.main(ExerciseWithString.java:21)

At this point, i suppose that the problem is in the class Parameters.
I'm going to post an excerpt.


to me the class looks ok. Maybe fresh eyes can see what it's hidden to me...
However, just to be thorough, i've tried to substitute the line
in class IndexOfVariants using the variable char be in the constructor GeneralMethods
so the line :

it's become:


The nullpointer ex is still there, but it changes the line of code that calls it.

Exception in thread "main" java.lang.NullPointerException
at cam.pippo.methodIndexOf.ChooseIndexOfParameters.<init>(ChooseIndexOfParameters.java:21)
at cam.pippo.methodIndexOf.IndexOfVariants.<init>(IndexOfVariants.java:11)
at cam.pippo.ExerciseWithString.go(ExerciseWithString.java:26)
at cam.pippo.ExerciseWithString.main(ExerciseWithString.java:21)

this is the line (21) that gets me the error in class ChooseIndexOfParameters
here there is only the constructor (the error is called at line 4):


Ok, that's all.
Sorry for the longness but i think it's the only way to be clear.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic