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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:54:26+00:00 2026-06-11T06:54:26+00:00

So I have this code : package Firstpack; import java.io.*; import java.util.*; public class

  • 0

So I have this code :

package Firstpack;

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

public class Main {

    public static void menu() {
        System.out.println("Welcome");
        System.out.println("1. Add a record ");
        System.out.println("2. See all records ");
        System.out.println("3. See a category");
        System.out.println("4. Total spend(Year)");
        System.out.println("5. Spend in a month");
        System.out.println("6. Chose by index");
        System.out.println("7. Exit ");

        System.out.print(">");
        Scanner in = new Scanner(System.in);
        int enteredInt = in.nextInt();
        in.close();

        switch (enteredInt) {
        case 1:
            recording();
            break;
        case 2:
            System.out.print(" You have chosen -> See all records");
            break;
        case 3:
            System.out.print(" You have chosen -> See a category");
            break;
        case 4:
            System.out.print(" You have chosen -> Total spend(Year)");
            break;
        case 5:
            System.out.print(" You have chosen -> Spend in a month");
            break;
        case 6:
            System.out.print(" You have chosen -> Chose by index");
            break;
        case 7:
            System.out.print(" Bye! ");
            break;
        default:
            menu();
        }

    }

    public static void recording() {
        System.out.println(" You have chosen -> Add a record");

        record rec = new record();
        Scanner in = new Scanner(System.in);

        System.out.print("Enter Amount > ");
        rec.amount = in.nextDouble();
        System.out.print("Enter Category > ");
        rec.category = in.next();
        System.out.print("Enter Details > ");
        rec.details = in.next();

        try {
            FileWriter fw = new FileWriter("findme.txt");
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(String.valueOf(rec.amount));
            bw.write(rec.category);
            bw.write(rec.details);

            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        menu();
    }
}

And it throws me this:

Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Unknown Source)
    at Firstpack.Main.recording(Main.java:68)
    at Firstpack.Main.menu(Main.java:25)
    at Firstpack.Main.main(Main.java:82)

And I have no idea how to solve this. What is wrong here?

  • 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-11T06:54:28+00:00Added an answer on June 11, 2026 at 6:54 am

    The problem is because of the call to in.close() in your menu method. Not only does in.close() close the Scanner object, but it also happens to close System.in as well. When you enter 1 as the menu selection your recording() method gets called which creates a new Scanner which tries to use the now closed System.in.

    This is a post that addresses a similar issue:
    Trying to write a method that checks user input in JAVA, getting NoSuchElementException

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: package picalculator; //import java.util.Scanner; public class PiCalculator { static int
I have this simple java code: package com.androiddesktoptest.testtest; public class AndroidDesktopTestMain { public static
I have this Code... ifsDAO.java package source; import java.sql.*; import java.sql.SQLException; import java.util.*; public
I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code: package graphics { import flash.display.Sprite; import flash.events.*; public class Ball
I have this code: package org.optimization.geneticAlgorithm; import org.optimization.geneticAlgorithm.selection.Pair; public abstract class Chromosome implements Comparable<Chromosome>
I have this code package com.cerbertek; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory;
I have this code: package com.example.pr; import android.media.MediaPlayer; public class Audio{ MediaPlayer mp; public
I am writing an updater. I have this code: package main; import java.io.*; import
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);

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.