• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception in servlet

 
Larry Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Larry Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My client side code

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic