Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:47:58+00:00 2026-05-28T07:47:58+00:00

I am trying to use the code shown in the following example: java.lang.NullPointerException while

  • 0

I am trying to use the code shown in the following example:

java.lang.NullPointerException while creating DiskFileItem

My Test method contains the following code:

final File TEST_FILE = new File("C:/my_text.txt");
final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile());
diskFileItem.getOutputStream();

System.out.println("diskFileItem.getString() = " + diskFileItem.getString());

The text file exists in this location but the last line in the above code does not output the file content.

Any idea why?

N.B.

The following does print the file content:

BufferedReader input =  new BufferedReader(new FileReader(TEST_FILE));
String line = null;
while (( line = input.readLine()) != null){
    System.out.println(line);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:47:58+00:00Added an answer on May 28, 2026 at 7:47 am

    In your first code snip you use an OutputStream and it doesn’t work. In the second part you use an InputStream (or whatever impl of this) and it works 🙂 You might want to try with getInputStream() instead… OutputStream is to write bytes not reading.

    http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/disk/DiskFileItem.html

    try this one, it’s simple and from scratch just to help :

    final File TEST_FILE = new File("D:/my_text.txt");
        //final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE);
        try
        {
            DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("fileData", "text/plain", true, TEST_FILE.getName());
            InputStream input =  new FileInputStream(TEST_FILE);
            OutputStream os = fileItem.getOutputStream();
            int ret = input.read();
            while ( ret != -1 )
            {
                os.write(ret);
                ret = input.read();
            }
            os.flush();
            System.out.println("diskFileItem.getString() = " + fileItem.getString());
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the following code but it's returning the wrong day of
I am trying to use the following code to write data into an excel
I'm trying to use jquery validate. Following the example: http://docs.jquery.com/Plugins/Validation works great and I'd
I am trying to use the following code to convert a hash of options
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
i am trying to use this code to bind my asp.net menu control to
I'm trying to use this code to replace spaces with _, it works for
I'm trying to use the code below to send messages via System.Net.Mail and am
i am trying to use this code: <%= File.ReadAllText(Server.MapPath(Members/newsletters/welcome.html))%> which works great but now
I am trying to use this code in my QT app QMap<QString,QMap> but there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.