File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Null pointer exception in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Null pointer exception in servlet" Watch "Null pointer exception in servlet" New topic
Author

Null pointer exception in servlet

Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Hi guys, i written a servlet function to incorporate a DH key exchange.
However i keep getting a null pointer exception on a BigInteger variable.
I know that you can`t initalize a BigInteger with a null variable , however i checked that the string that i`m initialising
it with contains a value.
As i am a novice in servlet writing, i`m rather confused what is going on with my code. Hope you guys can help me out here because i`m really stumped on this.

Thanks in advance

Below is my code snippet



and my log from the tomcat

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

SEVERE: Servlet.service() for servlet [DHServlet] in context with path [/MyServletProject] threw exception
java.lang.NullPointerException
at DHServlet.processRequest2(DHServlet.java:165)


Which line is 165?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Bear Bibeault wrote:SEVERE: Servlet.service() for servlet [DHServlet] in context with path [/MyServletProject] threw exception
java.lang.NullPointerException
at DHServlet.processRequest2(DHServlet.java:165)


Which line is 165?


this is the line
biPK = new BigInteger(pk2.toString());
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Larry Lee wrote:however i checked that the string that i`m initialising it with contains a value.

I see no evidence of code that does this checking.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18126
    
    8

Then your pk2 variable is null.

Here's where you assign a value to it:



but if there's no "publicK" parameter in the request then null is assigned to the variable. You'll want to put in some code which checks for that.

Also, there's no point in calling the toString() method of a String variable. Just use the variable itself.
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Paul Clapham wrote:Then your pk2 variable is null.

Here's where you assign a value to it:



but if there's no "publicK" parameter in the request then null is assigned to the variable. You'll want to put in some code which checks for that.

Also, there's no point in calling the toString() method of a String variable. Just use the variable itself.


the publicK is sent from my client application to the servlet. i shall update my first post with my client side code
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Bear Bibeault wrote:
Larry Lee wrote:however i checked that the string that i`m initialising it with contains a value.

I see no evidence of code that does this checking.


i just did a normal if statement for it

if([string-name] != null]
System.out.println("i`m not a null string");

i also printed the values out to ensure that they have values in them
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
My client side code

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

There's no such statement in your posted code.

Also, you need to do more than just print something out. What do you really want the servlet to do if the value is null? It's always a real possibility that your code must be able to handle.
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Bear Bibeault wrote:There's no such statement in your posted code.

Also, you need to do more than just print something out. What do you really want the servlet to do if the value is null? It's always a real possibility that your code must be able to handle.


My apologies, i deleted that section away previously.
I just factored it in for checking as i want to get the main functions out first
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Well, what are you going to do about it? Why did you remove the checking? It's an important part of the process.
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Bear Bibeault wrote:Well, what are you going to do about it? Why did you remove the checking? It's an important part of the process.


i will code it out. It was a moment of folly
But i still don`t understand why it threw such an exception, my client passed the variables to the server correctly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

It's always easy to think that something has happened. But the exception proves otherwise. Put the checking code into the servlet and see where things go from there.

But you haven't answered the question, what do you want to happen when a null value is detected?
Larry Lee
Greenhorn

Joined: Jul 12, 2013
Posts: 8
Bear Bibeault wrote:It's always easy to think that something has happened. But the exception proves otherwise. Put the checking code into the servlet and see where things go from there.

But you haven't answered the question, what do you want to happen when a null value is detected?


i am thinking of checking whether the public key of the server is null.
If it is null, i will ask for a re transmission.
If it is still null, i will assign it a random value and also convey it to the client side.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer exception in servlet
 
Similar Threads
Unable to update data into database
Error In JSP Page
To run a simple servlet in Jboss 5.1.0
how to call JSP from Servlet
javax.naming.NameNotFoundException