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

  • Home
  • SEARCH
  • 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 5951283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:30:53+00:00 2026-05-22T17:30:53+00:00

may i know why? i have passed in three strings to the addTab method

  • 0

may i know why? i have passed in three strings to the addTab method and it is there in the variable when i debug but it says it is null why is that so? i have also instantiated the arrayList

public class STFile implements Serializable{

    private ArrayList<String> qnsTitle;
    private ArrayList<String> qnsImagePath;
    private ArrayList<String> qnsSoundPath;
    private Boolean fileExist;
    //Constructor for STFile,gets existing data files if exists and load values from it to data files arraylists, if dont exist
    //arraylists for data file will be instantiated.

    public STFile()
    {
         setFileExists(checkIfAllFileExist());
         if(getFileExist())
         {
             try {
                setQnsTitle(STFile.readFile(STMain.TITLES_PATH));
                setQnsImagePath(STFile.readFile(STMain.IMAGES_PATH));
                setQnsSoundPath(STFile.readFile(STMain.SOUNDS_PATH));

            }catch(IOException e)
            {
                System.out.println("in class STFile, IOEXception");
            }catch(ClassNotFoundException e)
            {
                System.out.println("in class STFile, ClassNotFoundException");
            }
         }else
         {

             File titleFile = new File(STMain.TITLES_PATH);
             File imageFile = new File(STMain.IMAGES_PATH);
             File soundFile = new File(STMain.SOUNDS_PATH);
            qnsTitle = new ArrayList<String>();
            qnsImagePath = new ArrayList<String>();
            qnsSoundPath= new ArrayList<String>();
         }

    }

    public void addTab(String title,String imagePath,String soundPath)
    {
        getQnsTitle().add(title);
        getQnsImagePath().add(imagePath);
        getQnsSoundPath().add(soundPath);
        try {
            writeFiles();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("in STFile addtab Exception");
            e.printStackTrace();
        }
    }

public static ArrayList<String> readFile(String filePath) throws ClassNotFoundException, IOException
    {

                ArrayList<String> arraylist = new ArrayList<String>();
                ObjectInputStream obj_in = null;
                FileInputStream f_in = null;

                try {

                    f_in = new FileInputStream(filePath);
                    obj_in = new ObjectInputStream (f_in);
                    arraylist = (ArrayList<String>)obj_in.readObject();
                    return arraylist;
                }catch(Exception e){
                    return null;
                }finally{
                    f_in.close();
                    obj_in.close();
                    return null;
                }

    }

main method.

STFile file = new STFile();
    file.addTab("Title", "image", "sound");

it keeps throwing

Exception in thread "main" java.lang.NullPointerException
    at STFile.addTab(STFile.java:53)
    at STMain.main(STMain.java:18)
  • 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-22T17:30:54+00:00Added an answer on May 22, 2026 at 5:30 pm

    Your readFile method will always return null, because you’ve got return null; in your finally block.

    So if the file exists, qnsTitle (etc) will be null, causing the NullPointerException later.

    I would strongly advise you not to catch Exception in the way you’re doing in readFile, either. Only catch specific exceptions if you must do so at all – but in this case I wouldn’t in the first place, or possibly only to wrap it in a different exception. Simply returning null from the catch block is hiding the fact that something’s gone wrong, and introducing another problem further down the line. I suggest you just expand the throws clause of your method (e.g. to include IOException) and remove the catch block.

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

Sidebar

Related Questions

As many of you may already know I have been having issues with another
As you may know, when we have this code in Javascript : function getName()
1.i dont have iphone, so may i know does iphone come with pre-installed flash
I have never connected to a database in java before. May I know if
I wanted to know If there is some way I could use three touches
I am hoping that someone may have an idea on how to control/specify the
Suppose I have a number of related classes that all have a method like
You may know this recommendation from Microsoft about the use of exceptions in .NET:
You may know the scriptaculous SlideUp effect . Well, It slides up a div
As you may know, in VS 2008 ctrl + tab brings up a nifty

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.