jQuery in Action, 3rd edition
The moose likes Sockets and Internet Protocols and the fly likes Keep getting NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Keep getting NullPointerException" Watch "Keep getting NullPointerException" New topic

Keep getting NullPointerException

Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 116


I am trying to manually do a behind-the-scenes HTTP POST through telnet on port 8080 with TomCat 6 server running.
The problem is that it will not work.

I have the following servlet (BeerSelect.java):

and the following model Java class:

Everything works fine when I use http://localhost:8080/Beer-v1/form.html in my browser and hit 'submit'.
The resulting servlet-processed page is shown to me with the correct output:

Beer Selection Advice

try: Jail Pale Ale
try: Gout Scout

However, when I try to do a manual HTTP POST in telnet the following error is returned after 2x hit enter:

I am simply trying to pass a parameter to get the page to work.
Is it possible to do a POST in telnet and upon refreshing the page in the browser to get the post-resulted page?
Perhaps that cannot be done because when you refresh the page a simple GET would be done... hm.

What am I not understanding or doing wrong?


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63352

You might get better responses if you post a properly formatted stack trace.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Todd Buell

Joined: May 31, 2005
Posts: 21
I'm going to wager a guess that the request parameter isn't getting set when you're sending the telnet request. Make sure the parameter is getting passed in. The following will compile and look just fine but will cause an NPE when there is not a request parameter called "foo".

Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 116
Yes, I know that Todd.

Question is, how do I pass it in through telnet?
I am reading in the 'Head First Servlets & JSP' book that the parameters are simply beneath the headers in the request.
And so I wrote such in notepad:

I copied this literally from notepad and pasted it (right click, paste) into the cmd window.
I do not know why the parameter is not passed in. Perhaps something to do with the Enter not being literal. I do not know.

Meanwhile I will try to find out.
Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 116
Finally figured it out.

It appears I had to specify 'Content-Length' and 'Content-Type'.
The nail was 'Content-Length', because if you enter the wrong length, the server will ignore your commands.
So this is the working, fixed HTTP POST request:

As you can see the Content-Length is 10, because "color=dark" are ten characters.

Problem solved.


Ryan McClain
Ranch Hand

Joined: Nov 27, 2010
Posts: 116
This topic should be marked as resolved, so that other fellow internet users can look it up on Google and find it.

I agree. Here's the link: http://aspose.com/file-tools
subject: Keep getting NullPointerException
It's not a secret anymore!