I am really stuck at this exception from 3 days. It was working fine but suddenly it is throwing this exception.
I am trying to upload an image for cleaning from my android device to JAVA servlet in jboss which is like this :
Class Servlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("You need to post an image.");
PrintWriter out = response.getWriter();
out.println("Please post an image..");
out.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long time = System.currentTimeMillis(); // check the time, for file-naming and such
System.out.println("Got a post - " + time);
try{
// get the image from the app
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
byte[] imageBytes = (byte[]) ois.readObject();
PlanarImage image = ImageUtil.getAsImage(imageBytes);
ois.close();
System.out.println("Read image from stream...");
// save a copy for inspection
ImageUtil.saveImage(image, time + "_y");
.......
And the ImageUtil.getasimage is like :
public static PlanarImage getAsImage(byte[] imageBytes){
BufferedImage bi = null;
InputStream in = null;
try {
in = new ByteArrayInputStream(imageBytes);
bi = ImageIO.read(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return PlanarImage.wrapRenderedImage(bi);
}
I am getting the following error on the line
PlanarImage image = ImageUtil.getAsImage(imageBytes);
Error :
[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/VINCServer].[vinc]] (http--0.0.0.0-8080-2) Servlet.service() for servlet vinc threw exception: java.lang.ClassNotFoundException: javax.media.jai.ParameterBlockJAI from [Module "deployment.VINCServer.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
at VinCapture.doPost(VinCapture.java:41) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_55]
I have also downloaded all the needed jars for the above.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.usaa.ar.deconvolution.Restore;
import com.usaa.ar.util.ImageUtil;
But still no luck.Please help me as I am not able to find anything on this exception.
Related
I found an error org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException exception while deploying the war file.my code is as follows.
public class ReminderService extends HttpServlet implements Runnable{
public void init() throws ServletException {
LogMsg.info("I am init()");
ReminderService rThread = new ReminderService();
refresher = new Thread(rThread);
refresher.start();
LogMsg.info("I am from appointments refresh Method.Thread is started now");
}
#Override
public void run() {
try {
LogMsg.info("I am from run Method.");
obj.somefunction();
Thread.sleep(3000);
LogMsg.info("I am from run Method.Thread woke up from sleep");
}catch (InterruptedException e) {
e.printStackTrace();
}
run();
}
}
Here i want to call the this servlet class init() by default i mean while deploying the war file but the problem is it is not triggered automatically,please let me know i am new to this.
Here the problem is in run method when i tried to call any other methods it is raising the error as mentioned as below.Please let me know the wrong thing.Thanks in advance.
This is the message i got in console
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default- host].[/ThanQDoctor].[org.jboss.tools.thanqdoctor.rest.JaxRsActivator]] (http--127.0.0.1-8080-3) Servlet.service() for servlet org.jboss.tools.thanqdoctor.rest.JaxRsActivator threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_67] Caused by: java.lang.NullPointerException
Exception when writing the response back using jackson. Unable to find any solutions on the net. I am not sure what is the cause of error. Tried flushing the response but still the same error
import com.In10s.DBProperties.OracleDBConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
public class GetTable extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONObject jsonResponse = new JSONObject();
try {
JSONArray data = new JSONArray();
Connection con = OracleDBConnection.getConnection();
String query = "Select * from CREDIT_CARD_TRANSACTIONS";
Statement st = con.createStatement();
ResultSet rSet = st.executeQuery(query);
List<Map<String, Object>> result = new ArrayList<>();// JDK7++
while (rSet.next()) {
Map<String, Object> row = new HashMap<>();
row.put("CARD_NUMBER", rSet.getString("CARD_NUMBER"));
row.put("FIRST_NAME", rSet.getString("FIRST_NAME"));
row.put("OPENING_BALANCE", rSet.getString("OPENING_BALANCE"));
row.put("WITHDRAWAL", rSet.getString("WITHDRAWAL"));
row.put("DEPOSIT", rSet.getString("DEPOSIT"));
row.put("CLOSING_BAL", rSet.getString("CLOSING_BAL"));
row.put("TXDATE", rSet.getString("TXDATE"));
row.put("USAGE_TYPE", rSet.getString("USAGE_TYPE"));
result.add(row);
}
} catch (Exception e) {
e.printStackTrace();
}
ObjectMapper mapper = new ObjectMapper();
try {
response.getWriter().write(mapper.writeValueAsString(response));
response.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Here is stack trace:
com.fasterxml.jackson.databind.JsonMappingException: getWriter() has already been called for this response (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:190)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:671)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3383)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2777)
at com.In10s.getTable.GetTable.doGet(GetTable.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:644)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:536)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
... 28 more
You're trying to transform the HttpServletResponse into JSON, and send it to the HttpServletResponse. You want to transform the result list to JSON instead:
response.getWriter().write(mapper.writeValueAsString(result));
I have created an entity that selects all players from a database:
#NamedQueries({
#NamedQuery(name ="player.findAll",
query = "select p from Player p")
})
and then I have created a stateless bean and its interface that implements the method.
private EntityManager em;
...
public List<Player> findAllPlayers(){
TypedQuery <Player> query =
em.createNamedQuery("player.findAll", Player.class);
List<Player> results = query.getResultList();
return results;
}
and in interface its signature
public List<Player> findAllPlayers();
then I have created a stateless facade that works as a controller(MVC-structure).
#EJB
PlayerEAOLocal playerEAO;
...//Some more code
public List <Player> findAllPlayers(){
return playerEAO.findAllPlayers();
}
//And in interface...
public List <Player> findAllPlayers();
To make it clearer this is what I have done:
An entity
A local java bean(stateless) + Interface.
and a facade + Interface that refers to the beans.
Now I have created a servlet that the JSP will use for calling the FacadeLocal class(interface).
In my doPost method I have this implemented:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
FacadeLocal f = (FacadeLocal) facade.findAllPlayers();
request.setAttribute("facade", f);
}
and then I have the JSP file:
<form action = "GetAllPlayersServlet" method = "post">
<input type = "submit" name = "submit" value = "Show all"></input></td>
<td style = "width: 2px; "></td>
<% FacadeLocal facade = (FacadeLocal)request.getAttribute("facade"); %>
<td><input type = "text" name= "showAll" value = "<%= facade.findAllPlayers()%>">
But I get a nullPointerException
The error message
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/PlayerClientProject].[jsp]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: java.lang.NullPointerException
at org.apache.jsp.ShowAllPlayers_jsp._jspService(ShowAllPlayers_jsp.java:81)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
Alright. I am editing my question a little bit.
I have succeeded with showing all data from database in the servlet by doing:
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
List<Player> allPlayers = facade.findAllPlayers();
for (Player p1 : allPlayers) {
out.println("<h4>Founded: " + p1.getClass().getSimpleName());
out.println(" Id: " + p1.getId());
out.println(" - " + p1.getNickname());
out.println(" - " + p1.getLocation());
out.println(" - " + p1.getChampion() + "</h4>");
}
}
Now I wonder how I can pass the list to the JSP using doPost method?
You need to check facade for null:
<%
FacadeLocal facade = (FacadeLocal)request.getAttribute("facade");
if(facade == null)
{
out.print("<td><input type='text' name='showAll' value='' />");
}
else
{
out.print("<td><input type='text' name='showAll' value='"+facade.findAllPlayers()+"' />");
}
%>
If that doesn't fix it, do something similar everwhere you are using the dot operator on a variable that may be null.
People, please, help me!!! I am driving really crazy. My code have been working before today, and now it's broken...
Here is my servlet:
#Stateless
class TempSession {
void createFile( String fileName ) {
try {
FileWriter writer = new FileWriter( fileName );
writer.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
#WebServlet( name = "CreteFileServlet", urlPatterns = "/createCSV" )
public class CreateCSVConfigurationServlet extends javax.servlet.http.HttpServlet {
#Inject
TempSession session;
protected void doPost( javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {}
protected void doGet( javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {
session.createFile( "d:/session.txt" );
}
}
Here is web.xml:
<servlet>
<servlet-name>CreateFileServlet</servlet-name>
<servlet-class>com.bs.amg.test.servlets.CreateCSVConfigurationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateFileServlet</servlet-name>
<url-pattern>/createCSV</url-pattern>
</servlet-mapping>
JSP snippet:
<form action="createCSV" method="get">
<input type="submit" value="Create configuration"/>
</form>
Anв Jboss 7.0.1 log server snippet:
14:10:52,471 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "ear-1.0.ear"
14:11:10,336 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/web].[CreteFileServlet]] (http--127.0.0.1-8080-2) Servlet.service() for servlet CreteFileServlet threw exception: java.lang.NullPointerException
at com.bs.amg.test.servlets.CreateCSVConfigurationServlet.doGet(CreateCSVConfigurationServlet.java:136) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
Please, I really need your help!!!!!
You are declaring your servlet with both webservlet annotation and web.xml mapping. You should have only one declaration using the annotation as in this example.
I think that the injection is not occuring because CDI is not enabled. Make sure you have a beans.xml as stated in CDI documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html. Here is a sample beans.xml file you can use to enable CDI:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
I'm uploading an image to a servlet through an ajax post request (provided by a complex html5 drag and drop plugin's javascript code..which i won't show now)
In a first image upload everything is fine (i'm also sending some data within the ajax post for cropping the image in the server-side and this is the reason for which i had to convert the InputStream
gotten from the uploaded image to a BufferedImage (the conversion as following :
BufferedImage imBuff = ImageIO.read(fi.getContent());
This is the whole FileUploadServlet servlet :
package com.hmkcode;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.FileMeta;
import java.io.FileOutputStream;
import java.io.File;
//this to be used with Java Servlet 3.0 API
#MultipartConfig
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// this will store uploaded files
private static List<FileMeta> files = new LinkedList<FileMeta>();
/***************************************************
* URL: /upload
* doPost(): upload the files and other parameters
****************************************************/
private BufferedImage cropImage(BufferedImage src, int x, int y, int w, int h) {
BufferedImage dest = src.getSubimage(x, y, w, h);
return dest;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// 1. Upload File Using Java Servlet API
//files.addAll(MultipartRequestHandler.uploadByJavaServletAPI(request));
HttpServletRequest rq = request;
// 1. Upload File Using Apache FileUpload
files.addAll(MultipartRequestHandler.uploadByApacheFileUpload(rq));
// Remove some files
while(files.size() > 20)
{
files.remove(0);
}
//////////////////////////////////////////
System.out.println("Begin:::mytestsmytestsmytestsmytestsmytestsmytestsmytestsmytestsmytests");
if(files.size()==0)
System.out.println("files is empty !");
else
for(FileMeta fi : files){
System.out.println("------->>>> file name : "+fi.getFileName());
System.out.println("------->>>> file size : "+fi.getFileSize());
System.out.println("------->>>> file type : "+fi.getFileType());
System.out.println("------->>>> file Class : "+fi.getClass());
System.out.println("------->>>> file Content : "+fi.getContent());
System.out.println("______________________________________________");
///////////////// /////////////////////////////// /////////////////////////
// java cropping test :
BufferedImage imBuff = ImageIO.read(fi.getContent());
File outputfile = new File("C:\\uploadedimgs\\"+UUID.randomUUID()+".jpg");
ImageIO.write(cropImage(imBuff,
// 0,0,200,200
Integer.parseInt(MultipartRequestHandler.cropx1),
Integer.parseInt(MultipartRequestHandler.cropy1),
Integer.parseInt(MultipartRequestHandler.cropw),
Integer.parseInt(MultipartRequestHandler.croph)
), "jpg", outputfile);
////////////////////////////////
}
System.out.println("fin:::mytestsmytestsmytestsmytestsmytestsmytestsmytestsmytestsmytests");
// 2. Set response type to json
response.setContentType("application/json");
// 3. Convert List<FileMeta> into JSON format
ObjectMapper mapper = new ObjectMapper();
// 4. Send resutl to client
mapper.writeValue(response.getOutputStream(), files);
}
/***************************************************
* URL: /upload?f=value
* doGet(): get file of index "f" from List<FileMeta> as an attachment
****************************************************/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// 1. Get f from URL upload?f="?"
String value = request.getParameter("f");
// 2. Get the file of index "f" from the list "files"
FileMeta getFile = files.get(Integer.parseInt(value));
try {
// 3. Set the response content type = file content type
response.setContentType(getFile.getFileType());
// 4. Set header Content-disposition
response.setHeader("Content-disposition", "attachment; filename=\""+getFile.getFileName()+"\"");
// 5. Copy file inputstream to response outputstream
InputStream input = getFile.getContent();
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[1024*10];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.close();
input.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
Now the problem, which is a NPE, happens in a second upload try.It is the BufferedImage imBuff which is null. this is the exception log :
10:10:40,818 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/gencarservces-web].[upload]] (http-localhost-127.0.0.1-8080-6) "Servlet.service()" pour la servlet upload a généré une exception: java.lang.NullPointerException
at com.hmkcode.FileUploadServlet.cropImage(FileUploadServlet.java:40) [classes:]
at com.hmkcode.FileUploadServlet.doPost(FileUploadServlet.java:95) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_40]