为什么我收到“Error 500: java.lang.NullPointerException” java servlet

发布于 2025-01-04 20:11:00 字数 975 浏览 0 评论 0原文

我正在尝试将文件从我的 html 页面上传到我的 servlet 端代码并将其存储在 arraylist 中,

这里是 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>

。 。

。 。

这是我的 servlet 页面的 doGet() 方法中的内容

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);

,因此一旦我选择所需的文本文件并点击上传,我就会收到 nullpointerexception 错误。

帮助?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2025-01-11 20:11:01

因为当用户到达页面时,这是一个 GET 操作,因此根本不需要向页面提供任何数据。但您假设在代码中 getPart 未返回 null。然而,getPart 被明确定义为返回 null if "...此请求的类型为 multipart/form-data,但不包含请求的 Part ." (参考)。

您的表单被定义为使用 POST,因此您希望在 doPost 函数,不是您的doGet 函数。

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.

春夜浅 2025-01-11 20:11:01

有 2 个主要问题:

  1. 该代码必须位于 doPost() 方法内。不要混淆它们,也不要将其中一个称为另一个。这是明显糟糕的设计(是的,我知道大多数教程都是这样展示的,但这更多地讲述了教程本身)。另请参阅我们的 servlet wiki 页面,了解如何正确使用 servlet。

  2. 当您期望元素的名称为“textfile.txt”时,您尚未在输入元素上指定任何名称(这本身没有任何意义,您似乎期望上传文件的文件名自动成为输入元素名称,您认为如果最终用户选择具有不同名称的文件,它会如何工作?)。您需要以通常的方式为输入元素指定一个名称,以便您可以通过该名称准确地获取 Part。例如

    
    

    Part 部分 = request.getPart("上传");
    // ...
    

    您只需确保已输入 servlet 上的@MultipartConfig 注释。另请参阅 如何使用 JSP/ 将文件上传到服务器的底部Servlet?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文