0

I am attempting to upload a file from my html page to my servlet side code and store it in an arraylist

heres my html:

<pre>
<!DOCTYPE HTML>
<html>
<head>
<title>file upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="url to my servlet java code" method="post"  ENCTYPE="multipart/form-data">
<input type="file" value="browse..."/>
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
</pre>

. .

. .

heres what i have in my servlet page's doGet() method

Part p1 = request.getPart("textfile.txt");
Scanner in = new Scanner(p1.getInputStream());
ArrayList<String> newList = new ArrayList<String>();
while(in.hasNextLine()){
     newList.add(in.nextLine());
}
Collections.shuffle(newList);

so once i select the text file i want and hit upload, i get a nullpointerexception error.

help?

Jimmy Servlet
  • 155
  • 2
  • 3
  • 12
  • Are you assuming that textfile.txt is the uploaded file? – frictionlesspulley Feb 13 '12 at 22:33
  • Jimmy as James asked have you tried debugging? Did you have a peep at the stacktrace? On what line do you get the NPE? Did you see if `p1` is null? Some thoughts that comes to my mind. – MalsR Feb 13 '12 at 22:33
  • 1
    Shouldn't you rather put a name attribute on your input type=file and use getPart passing that same name? A little bit like you are getting parameters from your request using getParameter(). – Guillaume Polet Feb 13 '12 at 22:38
  • yes im assuming the .txt is the uploaded file. – Jimmy Servlet Feb 13 '12 at 23:57

2 Answers2

3

Because when the user arrives on the page, it's a GET operation, and so there's no requirement at all that any data has been provided to the page. But you're assuming in your code that getPart is not returning null. And yet, getPart is clearly defined as returning null if "... this request is of type multipart/form-data, but does not contain the requested Part." (ref).

Your form is defined as using POST, so you want to handle it in your doPost function, not your doGet function.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

There are 2 major problems:

  1. That code has got to be inside doPost() method. Do not mix them up nor call the one from the other on. This is plain bad design (yes, I know that most tutorials show them that way, but that tells more about the tutorial itself). See also our servlets wiki page to learn how to use servlets properly.

  2. You haven't specified any name on the input element while you're expecting that the element has the name of "textfile.txt" (which makes at its own no sense, you seem to be expecting that the filename of the uploaded file automagically becomes the input element name, how did you think that it would work if the enduser chooses a file with a different name?). You need to give the input element a name the usual way so that you can get the Part by exactly that name. E.g.

    <input type="file" name="upload" />
    

    with

    Part part = request.getPart("upload");
    // ...
    

    You only need to make sure that you've put @MultipartConfig annotation on the servlet. See also bottom part of How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Sure that you've put `@MultipartConfig` annotation on the servlet? See also bottom part of this answer: http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 – BalusC Feb 15 '12 at 20:18