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?
Because when the user arrives on the page, it’s a
GEToperation, and so there’s no requirement at all that any data has been provided to the page. But you’re assuming in your code thatgetPartis not returningnull. And yet,getPartis clearly defined as returningnullif “… 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 yourdoPostfunction, not yourdoGetfunction.