try / catch语句的自定义类,NPE内不工作 [英] Try/Catch not working inside a custom class, NPE

查看:169
本文介绍了try / catch语句的自定义类,NPE内不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单。我有一个大我的主要项目,并成为笨拙。所以今天我决定尝试让我的自己的类,这样我就可以简化在某些 code
所以这是我的小,我用了数据存储。在的try / catch 语句的工作时,浮动本身在大类。虽然在小类,它抛出一个NPE。看来,当我让我的自己的类中的try / catch系统无法工作。为什么有什么想法?

file_in = openFileInput(array_saved);

是logcat的叫了一声。

...这是类:

 包com.eai.thepicker;进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.io.ObjectInputStream中;
进口java.io.ObjectOutputStream中;
进口的java.util.ArrayList;
进口android.app.Activity;
公共类DataHandler的延伸活动{    FileOutputStream中file_out;
    的FileInputStream file_in;
    ObjectOutputStream的obj_out;
    ObjectInputStream的obj_in;
    ArrayList的<串GT; retrieved_data;    公众的DataHandler(){
    }    @燮pressWarnings(未登记)
    公众的ArrayList<串GT; retrieveData(){        尝试{
            file_in = openFileInput(array_saved);
            obj_in =新的ObjectInputStream(obj_in);
            如果(obj_in.available()大于0){
            retrieved_data =(ArrayList的<串GT;)obj_in.readObject();
            }
            其他{
                retrieved_data =新的ArrayList<串GT;();
               }            obj_in.close();
            file_in.close();
        }赶上(FileNotFoundException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }赶上(ClassNotFoundException的E){
            e.printStackTrace();
        }        返回retrieved_data;    }    公共无效SAVEDATA(ArrayList的<串GT; DATA_OUT){
        尝试{
            file_out = openFileOutput(array_saved,0);
            obj_out =新的ObjectOutputStream(file_out);
            obj_out.writeObject(DATA_OUT);
            obj_out.close();
            file_out.close();
        }赶上(FileNotFoundException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}


解决方案

我认为问题是,你试图调用此方法

  openFileInput()

没有给它背景它需要。我仍然不认为你需要延长活动而是通过去除使这个普通班延伸活动。创建这个类的一个实例,当你需要它在调用活动

  DataHandler的数据=的DataHandler(本); //提供上下文
data.retrieveData();

类似的东西要调用的方法。然后创建像

类的构造

  ublic类DataHandler的延伸活动{FileOutputStream中file_out;
的FileInputStream file_in;
ObjectOutputStream的obj_out;
ObjectInputStream的obj_in;
ArrayList的<串GT; retrieved_data;
上下文mContext; //添加上下文变量公众的DataHandler(上下文的背景下){
mContext =背景;分配上下文
}

然后在你的方法使用

 公开的ArrayList<串GT; retrieveData(){    尝试{
        file_in = mContext.openFileInput(array_saved);
        obj_in =新的ObjectInputStream(obj_in);
        如果(obj_in.available()大于0){
        retrieved_data =(ArrayList的<串GT;)obj_in.readObject();
        }
        其他{
            retrieved_data =新的ArrayList<串GT;();
            }        obj_in.close();
        file_in.close();
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }赶上(ClassNotFoundException的E){
        e.printStackTrace();
    }    返回retrieved_data;}

我相信这样的事情应该有所帮助。该方法 openFileInput()期待一个上下文在这一点上,因此 NPE 。传递一个上下文的类可以解决这个问题,因为它会产生电流活动上下文

<一个href=\"http://developer.android.com/reference/android/content/Context.html#openFileInput%28java.lang.String%29\"相对=nofollow>文档

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();
        }   
    }


}

解决方案

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

这篇关于try / catch语句的自定义类,NPE内不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆