wood burning stoves 2.0*
The moose likes Servlets and the fly likes Nullpointer 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 "Nullpointer exception in servlet" Watch "Nullpointer exception in servlet" New topic
Author

Nullpointer exception in servlet

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,
I tried to get the status of the extension in a servlet program using the command

I have declared status event like

private StatusEvent statusevent

please assist me to solve this problem.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi

StatusEvent object is not created in your program. You need to create an object of StatusEvent either by new operator or any factory method or by any other means, before you can invoke any method on that. Since there is no object existing at all, you are getting NullPointerException.

Thanks
Ravi


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your reply.sorry for the delayed response.what i need to do is: I am using asterisk java to make inbound and outbound calls.if i dial a number from my extension and run the following program,it should get the status of the extensions whether it is ringing ,busy,up etc...
but now i am getting the result null.i think i am doing mistake in creating object.my code follows.

Thanks.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

For which method invocation are you getting the NPE
Have you checked the exception stack trace.
I think the following code causes the problem

Either the factory object is null or managerConnection is null.


Hope this helps .


SCJP, SCWCD.
|Asking Good Questions|
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.I am in doubt that i have to extend the ManagerEventListener and have to add eventlistener like

But how shall i extend two classes at a time in this servlet program?

Thanks.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Create an instance of the class ManagerEventListener inside your servlet instead of extending the class. Post your Stacktrace for getting correct answer.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.If you dont mind could you show it in code
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have tried like this to create an instance for the class.is it correct?


It is showing the following exception

INFO: Determined Asterisk version: Asterisk 1.4
java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.example.servlets.OutboundServlet.doGet(OutboundServlet.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Yes you are correct. Check the following line no (83) in your servlet

at com.example.servlets.OutboundServlet.doGet(OutboundServlet.java:83)
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
the line is

Object obj = Class.forName(className).newInstance();

where i am wrong?
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Check the class mentioned in is an abstract class or interface. If it is an abstract class or interface. It will throw this exception.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
sorry.In the past code i didnt have posted you the code which creates instance.

here the class is org.asteriskjava.manager.AbstractManagerEventListener.The instantiation exception is in the line

Object obj = Class.forName(className).newInstance();

I have checked.This is not an interface.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
That is why you are getting instantiation exception. Your class is an abstract class. You cannot create instance for abstract class and interface
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Is there any other way to solve this?
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Extend the abstract AbstractManagerEventListener and create the instance of your concrete class using reflection. This is the solution. BTB, What your AbstractManagerEventListener is doing?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I am already extending httpservlet.then how can i extend this too
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Create a class named ConcreteManagerEventListener which extends AbstractManagerEventListener.

Then create an instance for your ConcreteManagerEventListener in your OutboundsServlet using reflection.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.But could you show me in code how to do this.I dont know how to do...
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Class ConcreteManagerEventListener extends AbstractManagerEventListener {
// do something


}


write the following in your OutboundServlet,

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have done like this:

when i run this,I am getting the following error:

HTTP Status 405 - HTTP method GET is not supported by this URL

--------------------------------------------------------------------------------

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).



please tell me where i have to change in code exactly.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
I would suggest you to write a separate java class instead of writing inside a servlet.
import the new class to the servlet.
create instance of the new class using reflection.

I think you might have invoked doPost instead of doGet. Check your form method.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334



In your servlet, you have to import com.example.servlets.ConcreteManagerEventListener . After importing the class, write the following

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thank you for this much of help.I have tried what you have instructed me to do.But now also i am getting the output as null.i am posting what i have done.


and the OutServlet code is:

could you point me where am i wrong?
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Do you get NPE or Null is printing? let me know how do you invoke the servlet?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Null is printing.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
I think, you are getting Null value in the statement


Can you post the code of getState() method?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi satish,Thank you so much for your help.Now i am getting the output when i am running the servlet.but i am getting the output in console only.how can i print it in webpage.
java code:


servlet code:


How do i print the output of the java program in the webpage???
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Thats great! You have an instance of PrintWriter in your servlet.Use PrintWriter instance to print the output in your browser

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
But i need to print the event status in the web page.I need to print a and b from the following which is written in the java code.

(ie)String a=event.getChannel();
String b=event.getState();
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Write a couple of methods in your java class

Declare the String variables as instance variables in your class


In your servlet, create a instance for your HelloEvents.

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have inserted the code given by you.but in the webpage it is printing null.
java code:

servlet code:
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
You have created a local String variables a and b in your method handleEvent. But you are returning channel & status in your getChannel & getStatus method.

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
i have replaced a and b with channel and status.but now also i am getting null in screen.
java code:


servlet code:
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hello Preethi,

I think you have not read my previous post clearly. You have declared two local variables channel & status in your method handleEvent and you are returning those values by getChannel & getStatus. This is the reason for getting Null value in the page. Declare these two variables channel and status as Instance Variable.


protected void handleEvent(StatusEvent event)
{

String channel=event.getChannel();
String status=event.getState();
System.out.println(channel + ":" +status);

}
public String getChannel() {
return channel;
}

public String getStatus() {
return status;
}
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
and write the handleEvent code like this

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
ya.i did like that too.but i am getting null still.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hello Preethi,

I didnt go through your code carefully . Please try the following


You have already created an instance using reflection. There is no need of creating instance of HelloEvents again Delete the bold faced line in the above code and try. I hope it would work
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thank you satish.its working fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nullpointer exception in servlet
 
Similar Threads
Spring 3 not calling .html and servlet is not available
taking a particular data from a number of lines
Hit A Servlet From an Applet with values in the URL for the Servlet
Swing design possible to integrate with servlet
Errors while adding IBM Java Examples 3.5.3