• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Jonutz Stan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I started learning Java 8 a few months ago. I don't have any prior experience in programming either.
The thing is that I am trying to make this program work. It is like a final exam for my Java class. And I get this Null Pointer Exception in the AgTurism class. I don't understand why. I put a comment in the code line where the exception occurs. The names are in Romanian, but I hope that you guys can understand what they mean. They are similar to English. And besides class Circuit, I also have a class Sejur, which is very similar, so I didn't put it here.

Thanks!















 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post full stack trace, so we could try to help you understand reading it in order to find out, which part is causing you trouble.
 
Jonutz Stan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for replying. I am using BlueJ, so I don't know how can I can get the stack trace. I think that is something Eclipse has.
The exception is in the first code block, line 28:   if(this.dest.equals(agTurism.dest)){ // EXCEPTION APPEARS HERE

Thank you again
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jonutz,

you have a no-argument constructor of your AgTurism class. If you create an instance with this constructor, then "dest" will be null, and so this.dest.equals(....) generates an NPE.
Now, I can't see if this scenario is happening, but can you check this out? If so, my advice would be to remove this constructor.

And, on behalf of Liutauras and all the others around here: welcome to the ranch!
 
Jonutz Stan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Thank you for replying. I tried to remove the no argument constructor from AgTurism class, but then class Oferte doesn't compile. On line 46, class Oferte, I can't delete the new instance of AgTurism(). The constructor asks for parameters. And if I give him parameters I won't be able to delete any instance that I want from the console. It's kinnda like a vicious circle.

And, yes, I'm glad to be here, on this forum, too. Learning new things it's much easier when you're not alone.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this method in the 'Oferte' class:

Here you invoke the no-argument constructor, next you try to remove that new instance from the list. That will give an NPE.
What you could do is, for instance,:

That should work.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, blundering here...

No, what I just wrote is nonsense, sorry.
Th 'AgTurism's in the list in 'Oferte' might contain instances for which 'dest' is null. Now, I have not spotted yet if this list is fed with instances created with the no-argument constructor, or even with the three-parameter-constructor where the parameter 'dest' is null. I'm trying out something now, but can you look through all of your code to see if you have such instances?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't found anything particular so far...

In your 'Oferte' class, you have this method:
public void addAgTurism(AgTurism newAgTurism) throws PreaMulteOferteException{

Can you add a line that prints out the variable 'newAgTurism' and see what 'dest' is?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonutz Stan wrote:The exception is in the first code block, line 28:   if(this.dest.equals(agTurism.dest)){ // EXCEPTION APPEARS HERE

So, technically could be nulls 'dest' or 'agTurism', but, since you have an if statement above, 'agTurism' as null candidate rolls out. So we are left with 'dest'.
Piet mentioned, which reasons can cause that.

1. You have no-arguments constructor, probably you need to remove that, because that puts object in an illegal state, hence happens what happens.
2. You have parameters constructor which does no validation for you, meaning, parameter can be passed null instead of wanted some sort of string value.
- you could add some check, if parameter dest not null - assign it, otherwise throw some exception as IllegalArgumentException.

Please narrow down a bit of code you have posted and try to extract for us the parts where you creating those objects of interest. And re-post that narrowed down version - that will help us understand you understand what is happening in your program.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonutz Stan wrote:I am using BlueJ, so I don't know how can I can get the stack trace. I think that is something Eclipse has.
The exception is in the first code block, line 28:   if(this.dest.equals(agTurism.dest)){ // EXCEPTION APPEARS HERE

How do you know exception is there? You supposed to see some error message printed by BlueJ, can you copy/paste it here?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Have a look at this method in the 'Oferte' class:

Here you invoke the no-argument constructor, next you try to remove that new instance from the list. That will give an NPE.
What you could do is, for instance,:

That should work.

I thought that 'list.remove(p)' did, for i = first element to last, something like : if (i.equals(p)) ....
Just read the API, it does a 'if (p.equals(i))'.
So, that means that my reply is not nonsense after all...
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonutz Stan wrote:. . . I am using BlueJ, so I don't know how can I can get the stack trace.
I have tried BlueJ, and hated it roundly. Try running your code from the command line.
. . . The exception is in the first code block, line 28:   if(this.dest.equals(agTurism.dest)){ // EXCEPTION APPEARS HERE . . .
That is part of the stack trace; it supplies the information we need

In your equals() method, I presume you are going to write something which returns true. If there is a risk of there being a null in any of your fields, write yourself a utility class like this:-Then use that equals method
...MyUtilities.equals(this.dest, agTurism.dest)...
I won't tell you where you can find a ready‑made method which does the same thing but has a different implementation.
Start an equals() method with an equality test. If you use instanceof, the test about whether object is null is probably unnecessary.If you only use one return, it is slightly more difficult, but still possible, to avoid multiple (casts). Those equals() methods all depend on the fields having correctly‑overridden equals() methods; if dest is a String, that does have an equals() method.

...and welcome to the Ranch again
 
Jonutz Stan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again guys,

Thank you for all your replies.

Indeed, I deleted the no arguments constructor and I don't get NPE anymore.
Now, though, I can't delete from the ArrayList. I think it has something to do with the equals method. But it doesn't make any sense. I am using an example from my teacher when I am doing this, and in her case everything worked fine. She uses the same methods. I also tried encapsulation, to be sure that there is only one String dest. But it didn't work. I get the message that those strings are not ==.

So, in class Oferte I have this code


and in the main this



When I try to delete, I always get error. And I don't get it, it is the same String.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonutz Stan wrote:(...)
When I try to delete, I always get error. And I don't get it, it is the same String.

Well, the most likely cause of getting 'error' is that there is no AgTurism instance with dest equalling word[1].
So, how did you test whether the string is the same? In my simplified code yesterday (same 'equals' method) the removal went without any problem.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true or == false which are very error‑prone.
Not if (vez==true)...
but if (vez)...
Not if (vez==false)...
but if (!vez)...

What does that remove method return?
Have a look at the Java™ Tutorials, looking particularly at the sections about Lists, and the List interface and ArrayList. You will find that remove uses the equals() method to determine which element in the List to remove.

When you try to remove an AgTurism object, you appear to pass the dest (presumably destination) and price and duration of 0. Will those 0s match any of the existing elements?
If you are using Java8 find out about this method.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals method in the original post only seems to check the 'dest' value, so different price and duration won't matter.

Unless I missed a change in the code elsewhere in the thread.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed:

in your 'AgTurism' class in your opening post you have this 'equals' method:

Is this still your 'equals' method? If so, can you see why this method always returns false?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had thought there would be something else in the if where it says // EXCEPTION…, but you have a good point. If that block remains empty, it will fail to compile as not all paths return a boolean.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like enough.

If I were an OP, I'd stop any further debuggings and would start by writing tests to see if classes and their methods do what they are tend to do.

Aplicatie class has one method, which is main(), unfortunately it is clear already without any tests - it is wrong as it does something else than main() method supposed to be doing. That routine within main method usually needs to be very short, max 2 lines, preferably 1.

Then, would introduce another method within Aplicatie class which might would name run() and would lay all logic down there. Of course not that much, there supposed to be many helper methods which should help run() method to do all that stuff.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!