Try/Catch not working inside a custom class, NPE

拈花ヽ惹草 提交于 2019-12-11 23:52:17

问题


Fairly simple. I have a BIG class for my main project, and it became "unwieldy". SO today I decided to try making my own class, so I could simplify some of the code in the big class. So this is my "little" class, I used for the Data storage. The TRY/CATCH statement works when floating by itself in the BIG class. In the little class though, it throws an NPE. It seems the Try/Catch system isn't working when I make my own class. Any thoughts on why?

file_in = openFileInput("array_saved");

Is called out by Logcat.

...and this is the class:

package com.eai.thepicker;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;    
import android.app.Activity;


public class DataHandler extends Activity {

    FileOutputStream file_out;
    FileInputStream file_in;
    ObjectOutputStream obj_out;
    ObjectInputStream obj_in;
    ArrayList<String> retrieved_data;

    public DataHandler(){
    }

    @SuppressWarnings("unchecked")
    public ArrayList<String> retrieveData(){

        try {   
            file_in = openFileInput("array_saved");
            obj_in = new ObjectInputStream(obj_in);
            if(obj_in.available() > 0){
            retrieved_data = (ArrayList<String>) obj_in.readObject();
            }
            else{
                retrieved_data = new ArrayList<String>();
               }

            obj_in.close();
            file_in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return retrieved_data;

    }

    public void saveData(ArrayList<String> data_out){
        try {
            file_out = openFileOutput("array_saved", 0);
            obj_out = new ObjectOutputStream(file_out);
            obj_out.writeObject(data_out);
            obj_out.close();
            file_out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }


}

回答1:


I believe the problem is that you are trying to call this method

openFileInput()

without giving it context which it needs. I still don't think you need to extend Activity but make this a regular class by removing extends Activity. Create an instance of this class when you need it in the calling Activity like

DataHandler data = DataHandler (this);  // give context
data.retrieveData();

something like that to call the method. Then create a constructor in the class like

ublic class DataHandler extends Activity {

FileOutputStream file_out;
FileInputStream file_in;
ObjectOutputStream obj_out;
ObjectInputStream obj_in;
ArrayList<String> retrieved_data;
Context mContext;   // add Context variable

public DataHandler(Context context){
mContext = context;   assign context
}

Then in your method use

 public ArrayList<String> retrieveData(){

    try {   
        file_in = mContext.openFileInput("array_saved");
        obj_in = new ObjectInputStream(obj_in);
        if(obj_in.available() > 0){
        retrieved_data = (ArrayList<String>) obj_in.readObject();
        }
        else{
            retrieved_data = new ArrayList<String>();
            }

        obj_in.close();
        file_in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return retrieved_data;

}

I believe something like this should help. The method openFileInput() is expecting a Context which is null at this point, hence the NPE. Passing a Context to the class will solve this problem as it will have the current Activity Context

Docs



来源:https://stackoverflow.com/questions/16006486/try-catch-not-working-inside-a-custom-class-npe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!