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 9053829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:31:31+00:00 2026-06-16T13:31:31+00:00

Here is the code I have. This is my PrintToFile class import java.util.*; import

  • 0

Here is the code I have.

This is my PrintToFile class

import java.util.*;
import java.io.*;

public class PrintToFile{
        File f;
        FileWriter fw;
        PrintWriter pw;

    public void PrintToFile()throws Exception{//remove void from constructor
      File f = new File ("Output.txt");//dont reinitialize 
      FileWriter fw = new FileWriter(f, true);//dont reinitialize 
     PrintWriter pw = new PrintWriter(fw);//dont reinitialize 
   }

    public void printExp(ArrayList<Expense> expList){
        for(int i = 0; i < expList.size(); i++){
         pw.println("---------------------------------------");//exception here
         pw.println(expList.get(i));
      }
        pw.close();
    }
}

in my main class here is my call to print my ArrayList

    PrintToFile printer = new PrintToFile();
    printer.printExp(expList);   

I have defined expList as an ArrayList of objects

The exception I get is a

Exception in thread "main" java.lang.NullPointerException

occuring where marked. My question is what is causing this exception? Thanks

  • 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-06-16T13:31:32+00:00Added an answer on June 16, 2026 at 1:31 pm

    You are not creating object of pw which is class field but you are creating object of pw which is local to method PrintToFile(). So by default PrintToFile.pw is null and you get NPE.

    Change your method to following or initialize pw,f and fw in constructor(recommended):

    public void PrintToFile() throws Exception {
          f = new File ("Output.txt");
          fw = new FileWriter(f, true);
          pw = new PrintWriter(fw);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code here: public class clsDataLayer { // This function saves the
So I have this code here : int n; public static void Main(string[] args)
I have this code here: public static String AddRemoveDays(String date, int days) throws ParseException
So I have this code here: <table> <tr> <td width=200px valign=top> <div class=left_menu> <div
I have this code in java : try{ //some code here.. . } catch
Hi I have this code here: public function length($args) { if (isset($this->length)) { foreach
So I have this code here, that adds my NamePrinter runnable class as ExecutorService
I have this code here ToggleButton toggleAlarm = (ToggleButton) d.findViewById(R.id.toggle_alarm); toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() { public
I have this code here within a class: function getRolePerms($role) { if (is_array($role)) {
I have this code here import subprocess from time import strftime # Load just

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.