wood burning stoves 2.0*
The moose likes Servlets and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Null Pointer Exception

Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I Have a JSP form which cals servlet, that servelt uploads file in directory and insert other values of form in database. My uploading of files is working perfect, in that servlet now i am trying to call values of form through getParameter to insert in database. But it oesn't get the values and prints NULL.
please help
This is my Form
<form name= "projectinfo" method ="Post" action="saveFiles.servlet" title="theEventForm" ENCtype="multipart/form-data">
<input type="HIDDEN" name="Project" value="<%=projectname%>" size="100"/>
<input type="HIDDEN" name="Event_type" value="<%=eventtype%>" size="100"/>
<table border="0" cellspacing="4" cellpadding="0">


<tr>
<td align="right" width='200'>Event Type: </td>
<td align="left">
<select name='eventtype' onchange='eventtypeselect(this)'>
<option value=''></option>
<option value=''>TechPreScreen</option>
<option value=''>Passport1</span></option>
<option value=''>Passport2</option>
<option value=''>Passport3</option>
<option value=''>Passport4</option>
<option value=''>DesignReview</span></option>

</select>
</td>
</tr>

<%
// Declare some variables to use across multiple java blocks in this form
String attendees = "" ;
String notehistory ="";
boolean goteventtype = false ;
ProjectEvent pe = null;
String [] filenames = new String[0];


if ( eventtype != null && projectname !=null)
{
pe = db.getEventType(eventtype,projectname);
if (pe != null)
{
filenames = pe.getFileNames();
attendees = pe.getParticipants();
notehistory =pe.getNoteHistory();
goteventtype = true;
}

}


%>
</td>

</tr>
<tr>
<td align="right">Key contacts or participants:</td>
<td align ="left"><textarea name="participants" cols='45' rows='1'><% out.write (attendees);%></textarea></td>
</tr>
<tr>

<td align="right">IT Architecture Approved ?: </td>
<td align="left" >
<input type="checkbox" name='approved' checked="checked">
</td>



</tr>
<tr>
<td align="right">Notes/Issues/Open items: </td>
<td align="left"> <textarea name="notes" cols='50' rows='1'></textarea>


</td>

</tr>
<tr>
<td align="right">Notes History: </td>
<td align="left"><textarea name="notehistory" cols='50' rows='2' disabled='true'><% out.write(notehistory); %></textarea></td>



</tr>


<tr>
<table>

<td align="right">Attachments:</td>
<td align="left">

<table>
<tr><td>
<%
int filesdisplay = filenames.length;
for(int i =0;i<filesdisplay;i++)
{
out.write("<a href='/itads/docs/" + projectname + "/" + eventtype +"/"+filenames[i]+"'>" + filenames[i] +"</a><br>");
}



%>

</td></tr>

</table>

<table id="theAttachmentTable" border="0" cellspacing="0" cellpadding="0">
<tr><td><input type="FILE" name="FILE1" size="80" onchange="javascript:addNewAttachmentField();"/></td></tr>
</table>
</td></tr>
<td></td>
</table>

<tr>
<td align="center">
<input type="button" name="refresh" value='Refresh'>
</td>
<td align="center">
<td align="center">
<input type="submit" name="savedoc" value="Save ">
</td>


</tr>
</table>
</form>
</td>
</tr>
</table>

</body>
</html>
This is my Servlet
package servlet;
import DBAccess.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.naming.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
public class saveFiles extends HttpServlet {
/*boolean p = false;*/
char squote ='\'';
char dquote ='\'';

public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
ServletContext context = getServletContext();
ServletConfig config = getServletConfig();
String sessionId = request.getSession().getId();
java.io.File tempdir = (java.io.File) context.getAttribute( "javax.servlet.context.tempdir" );
out.write( "<html>" );
out.write( "<head>" );
out.write( "<title>Upload Files Servlet [" + request.getServerName() + "]</title>" );
out.write( "</head>" );
out.write( "<body>" );
ResourceBundle rb = ResourceBundle.getBundle("Project");
String rootdir_name = rb.getString("projectDocRootDirectory");
if ( rootdir_name.equals("TEMP") )
{
rootdir_name = tempdir.getPath() + "\\upload";
}
RequestDispatcher dispatcher = context.getRequestDispatcher( "/header.jsp" );;
dispatcher.include(request, response);
out.write( "<h1>Upload Files!</h1>" );
int count=0;
com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.SmartUpload();
mySmartUpload.initialize(config,request,response);
//mySmartUpload.setTotalMaxFileSize(10000000);
//mySmartUpload.setMaxFileSize(10000000);
mySmartUpload.upload();
String project_name = mySmartUpload.getRequest().getParameter("Project");
String eventtype_name = mySmartUpload.getRequest().getParameter("Event_type");
String subdir_name=project_name +"/"+ eventtype_name;
if ( subdir_name == null )
{
subdir_name = ".";
}
String dir_name = rootdir_name + "/" + subdir_name;
System.out.println(dir_name);
java.io.File dir = (java.io.File) new java.io.File( dir_name );
if ( !dir.exists() )
{
dir.mkdirs();
}
count = mySmartUpload.save( dir.getPath() );
out.println( "<p>" + count + " file(s) uploaded to " + dir.getPath() + ".</p>" );

out.println( "<ul>" );
com.jspsmart.upload.Files files = mySmartUpload.getFiles();
out.println( "<ul>" );
for ( int i=0; i<count; i++ )
{
com.jspsmart.upload.File f = files.getFile(i);
out.println( "<li>" + f.getFileName() + " [" + f.getSize() + " bytes]</li>" );
}
out.println( "</ul>" );
dispatcher = context.getRequestDispatcher( "/footer.jsp" );
dispatcher.include(request, response);
out.write( "</body>" );
out.write( "</html>" );
}
catch (Exception e) {
log(e.getMessage());
e.printStackTrace();
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String Attendees = request.getParameter("participants");
String NoteHistory = request.getParameter("notehistory");
System.out.println(Attendees); /* this print null on console*/
System.out.println(NoteHistory);/* this prints null on console*/
ProjectDatabase db = new ProjectDatabase();
String ANames =db.changeQuote(Attendees,squote,dquote);
String Notes =db.changeQuote(NoteHistory,squote,dquote);
db.createEvent(ANames,Notes);
doGet(request, response);
}
}
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
try


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
The code which told is already working fine, I have problem here
String Attendees = request.getParameter("participants");
String NoteHistory = request.getParameter("notehistory");
System.out.println(Attendees); /* this print null on console*/
System.out.println(NoteHistory);/* this prints null on conso
This code is in my servlet and when i try to print its value, it prints null.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Looking at the documentation for request.getParameter( String ) we see that it returns null if there is no parameter by that name in the request.
Why don't you dump all of the parameters in the request to see what is really there. The Tomcat distribution contains serveral example servlets that dump request parameters you can use as an example.
Bill


Java Resources at www.wbrogden.com
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
ok so go the other way and try
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
String Attendees = mySmartUpload.getRequest().getParameter("participants");
String NoteHistory = mySmartUpload.getRequest().getParameter("notehistory");
I can't try this code, because this part was used for uploading files in the directory.
I am using this request.getParameter(String)in my other servlets and it is working fine. I have 2 textareas with name "partcipants" and "notehistory" in my JSP form and those values i am trying to pass in servelet and to check if the values passed, it prints NULL on console.
I have pasted my servlet and form code above.
Please help.
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
ok so why are you doing doPost and doGet at the same time, this seems very wierd. you can easily move the code from doPost to doGet or visa-versa. my guess was that the file upload script was trashing the input stream and so the parameters were disappearing. if this seems possible move your code so that you can use the uploads own request object.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
Ok i am calling doGet method from doPost, but now inserting in database i have to put the code in doPost method, so then now where should i place my code which is in doPost method which get the values in the form and call few methods. how to sepearte them.
I want that servelt should 2 things , saves the files in the directory and insert other vlaues in database, is there any other way to do this which i am trying to do.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
Can anyone give me sugesstion on my problem which i wrote below.
Thanks
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
Well you can just copy the database code to the end of your doGet method. And then you can use the

String Attendees = mySmartUpload.getRequest().getParameter("participants");
String NoteHistory = mySmartUpload.getRequest().getParameter("notehistory");
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I didn't follow all the examples above, but ... when you do a Multi-Part mime post for the file upload the other parameters off your form come as "mime parts" and may not be put directly into the request. Can you iterate the parts that were uploaded and maybe find the part you need? I've done this, but only in a scratch-built Java server, not in J2EE.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Hi,

You may use apache's fileUpload utility for this. You can use parameters and file data on the server-side with it.

Thanks,

Maki
[ October 02, 2006: Message edited by: Maki Jav ]

Help gets you when you need it!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Maki Jav:
Hi,

You may use apache's fileUpload utility for this. You can use parameters and file data on the server-side with it.
Thanks,
Maki


Maki,
This thread is almost 3 years old.
It's pretty safe to assume that this issue was solved a long time ago.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception
 
Similar Threads
Getting "not a multipart request" even though the image is being saved.
Resetting Values in JSP Page
JSP Help Regarding the Radio Button
Passing more than one object between jsp and servlet
Returning a result set in an html form