• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException, Server and Client

 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Man, now I need to take a break :-)

From a test client (a GUI will replace this guy though), I call a 'write' method on my 'ServerImpl' class, which in turns calls a write method in the 'Data' class. I'm transferring a Value (Transfer) Object (called: 'Subcontractor') this whole way. In the 'Data' class when I do a Subcontractor.toString() (on the object though) it prints all the info for me beautifully, but as soon as I call the getName() (or whatever other method) on the Subcontractor object, I then get a NullPointerException.

Ps: Just for more info: at this stage I'm not creating a Thread as yet, it is purely a straight-forward call from the Client-to-the-server to insert something.

Hope this makes sense.

Have a great Sunday,
Pieter
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

OK, I just checked if the object that was returned by Data, if I could call methods on the Subcontractor object and yes that is working. Seems that the problem lies with the transfer back from the server to the client.

Ps: Something else; I am actually not passing back the Subcontractor object, but a List (ArrayList) <Subcontractor>.

Ciao,
Pieter
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,

So in your Data class you call subcontractor.toString() and the info is printed, but when you call instead subcontractor.getName() you get a NPE? That's the weird situation if I correctly understand your explanation.

Does the NPE occur on the line where you call the getName-method on the subcontractor instance? Or does the NPE occurs in the getName method itself ? And what happens if you put a call to the getName() method in the toString() implementation?

Kind regards,
Roel
 
Carlos Morillo
Ranch Hand
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,


If you are using Eclipse I'd strongly recommend you add some breakpoints in the client code around where
you get the NullPointerException and use the Debug perspective.

Personally, the minor bugs so far I have ran into my assignment have been solved by calling String trim().
They have had to do with extra space " " characters in the Strings processing in my code.

If you are using other IDE like NetBeans do the same and run your test case step by step and look at the content
of your variables once you get the NullPointerException.


HTH,


Carlos.
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for the speedy reply!

I actually mixed up a write method and find method in my post. I have tried two different methods now;
1- When I make a call from a client, it has an instance of Subcontractor. At the Server (ServerImpl class), I purely make a call to Data with the forwarded subcontractor. Within the Data class, I can call toString() on the subcontractor object and it works fine, but as soon as I call another method (e.g. getName) on the subcontractor object I get the NPE.

2- When I make a call from a client with a String[] containing the search criteria, the server forwards it to the Data class and get back a List<Subcontractor> (ArrayList). If I call toString and any of the methods on one of the objects in the List while in the server it works fine. The server (so by the way) returns the List of Sucontractors to the Client. If I call toString on any of the objects in the List from the Client I get the necessary info back, but as soon as I call any other method, I get the NPE.

For info:
- In my 'Client', I create a new Thread and in the run() connect to the server and do the above mentioned call/s.
- In my 'ServerImpl' class, I have a private variable database (Data.getInstance()) and then in the methods as decribed above, forward calls to database.
- Subcontractor is the Value Object with all variables as private and with getter and setter methods to get and set the values.
- All the methods in my Data class are marked as being synchronized. (I know the Data class is cool, I've expanded that test class and added a load of extra methods).
- I have a RemoteInterface, another interface with methods that needs to be implemented in the ServerImpl class.
- I have a class that I then use to start up the server.

I think this about sums this section up.

Thanks bud,
Pieter
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carlos,

Thanks for the reply!

I'm actually not using an IDE, I just use Notepad++ for the syntax highlighting and then compile using cmd.

If I can't manage to fix this issue, I will then install NetBeans or so.

Thanks,
Pieter
 
Carlos Morillo
Ranch Hand
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,


I'm actually not using an IDE, I just use Notepad++ for the syntax highlighting and then compile using cmd.


That's something of personal preference, BUT you have no idea how much time using an IDE like Eclipse or NetBeans can
save you in so MANY tasks during the development of the SCJD assignment project.

Piece of advise, install it right now. After using an IDE I don't ever ever see myself using vi or a text editor to develop software.
The difference is so huge, it's like day and night.



HTH,


Carlos.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,

Your subcontractor object implements Serializable I presume. Did you have any data member which you marked transient (a logger for example)?

And you didn't answer any of my questions:
Does the NPE occur on the line where you call the getName-method on the subcontractor instance? Or does the NPE occurs in the getName method itself ? And what happens if you put a call to the getName() method in the toString() implementation?


And about using Notepad++ and cmd for compiling and running your application: when I was at school my computer wasn't powerful enough to run an ide tool (like netbeans or jbuilder), so I developed with notepad and used java and javac for running and compiling the little programs. And I think every developer should have such an experience: you learn very quickly the java api (because no code completion, excellent for scjp certification ) and you learn also a whole lot about classpath issues when trying to compile and run your application/program. Of course there are disadvantages too: slower and less error-prone (because no code completion), no classpath issues,...
But for your SCJD you should just use an IDE like Eclipse or NetBeans, I use eclipse all the time, because like Carlos said it is a real time saver and it makes your life a lot easier. A tool like Eclipse has a local history for example: so if you have changed a file and not happy with the changes, you can easily rollback to a previous version. And testing what a small code change does to your application (like I suggested: adding call to getName in the toString method") is executed before you can say "pie" or "cheese" or "Supercalifragilisticexpialidocious"

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel & Carlos,

Thanks for the replies!

Sorry Roel, I was actually planning on answering you:

1- If I call getName() from toString(), I get the NPE still.
2- Here is the stackTrace for the above mentioned test:



Thanks chaps,
Pieter
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And can you show me the code of the getName-method?
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool,




Concerning your question regarding "transient" - yes, I have only used "transient" for the Logger.

Thanks,
Pieter
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pieter Jacobs wrote:Concerning your question regarding "transient" - yes, I have only used "transient" for the Logger.

Exactly your problem: transient members are not serialized, so your logger instance is null and the 1st line of the getName-method throws the NPE. This is something you should know, because you passed the SCJP certification and this issue is certainly addressed in the K&B Book

Solution: implement readObject (to customize the serialization process) and create the logger instance again:


Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thank you very very much!

And you know what, you are right, and I do know this but got caught up with the issue and didn't think for a moment that the issue is in Subcontractor (because not much is happening in it).

Thanks again,
Pieter
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pieter Jacobs wrote:didn't think for a moment that the issue is in Subcontractor

Although I gave a hint in that direction (Does the NPE occur on the line where you call the getName-method on the subcontractor instance? Or does the NPE occurs in the getName method itself ?) in my 1st post

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic