• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting null pointer error in JUnit test  RSS feed

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NullPointerException
at onplanusermodule.TimeSeries.getTimeSeries(TimeSeries.java:23)
at onplanusermodule.OnPlanUserModule.ProcessEmployees(OnPlanUserModule.java:58)
at onplanusermodule.OnPlanUserModuleTest.testProcessEmployees(OnPlanUserModuleTest.java:176)


Time Series


OnPlanUserModule



OnPlanUserModuleTest

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of spoilage in your code. But do you have any questions?
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is why am I getting a null pointer error as shown at top of post
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by identifying the line causing the exception. Then list every reference mentioned in that line, and work out which is null.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error tells you almost everything. On line 23 of your TimeSeries class, you're dereferencing null. Since that line doesn't contain a method call, and the only other variable is a primitive, it means timeSeries is null. You never initialize it.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan,
Thanks for responding. I think I see what you are saying. I thought it would be initialized by me externally calling setTimeSeries first.
Would it make sense for me to make setTimeSeries private and call it from the constructor?
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely. you should always bring your object in a consistent state I the constructor. Don't rely on other classes to do it for you.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testcase: testProcessEmployees(onplanusermodule.OnPlanUserModuleTest): Caused an ERROR
null
java.lang.NullPointerException
at onplanusermodule.TimeSeries.getTimeSeries(TimeSeries.java:28)
at onplanusermodule.OnPlanUserModule.ProcessEmployees(OnPlanUserModule.java:58)
at onplanusermodule.OnPlanUserModuleTest.testProcessEmployees(OnPlanUserModuleTest.java:171)

I tried code below (lines 26-28)which I thought would intialize the time series but still get similar error
I decided this would be better than using the constructor.
It seems like this code should work but it doesn't


 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I determined that I was getting an array out of bounds error
so added 19 to constructor
and the code to initialize the time series  aT LINES 26-28 seems to work.

Unfortunately I am getting a different error now in a different module which I will deal with tomorrow.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an out of bounds exception for the same array? Go back a week and look at this question of mine. If you are getting an out of bounds exception, you may be able to sort that out by answering my question.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method starting on line 24 mightn't cause an exception to be thrown, but it is inefficient. You are creating an array, only using one element, and throwing the rest away.

Actually there is a way you can get an exception out of that method; my first impression was mistaken. You can probably obviate the exception by making the method more efficient.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

I've said yesterday there are spoilage in your code, here is what I meant:

1. Since you have a class named TimeSeries, to let this class to have a field LocalDate timeSeries[] is misleading in my opinion. Let me explain you why:

Imagine I have a class Train

Doesn't it look weird? Train HAS-A train. It is essentially same as in your case.

Now consider this example:

Do you get an idea? Vagons form a train. Or Train is consisted of vagons. But not Train has train's. In your case TimeSeries has time series. Is that right? I'd be doubted. So my suggestion is, to define that Singular Thing which makes TimeSeries as a whole.

2. Since your class is named TimeSeries, to have a field timesSeriesDateStart is repeating same stuff over and over. It would be enough and less confusing if field would be named startDate. My point is, since it is field of TimeSeries class, it makes perfect sense and should be obvious that it is a start date of time series. If it isn't, then this field doesn't belong to that class.

3. Remove all the comments you have. They are all useless or misleading. i.e. // constructors - I see only one of those there.

4. Remove all excessive empty lines and improve indentation. Put some spaces around operators: + - < =... That way your code will be come more readable.

There are more things to mention, but I have no time at the moment.

When you write code, try to be disciplined and quite pedantic, trust me, that helps tremendously keep yourself on the track.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect most of that code to be in the constructor, as the constructor seems to be defined to set up the time series based on the parameters used in that loop (timeSeriesDataStart and periods).

ETA: Just to add, I would expect getTimeSeries to look like this:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method of DT's will be more efficient, but is still prone to throwing exceptions.
One way of getting round that is to add some documentation comments:-Be sure to put the right figures in for that range. Then it will be the user's fault if they suffer an exception, for not following the published instructions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!