Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Null Pointer Exception

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
try
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ok so go the other way and try
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can anyone give me sugesstion on my problem which i wrote below.
Thanks
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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");
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic