NullPointerException异常从一个类继承函数 [英] NullPointerException with inherited function from a class

查看:245
本文介绍了NullPointerException异常从一个类继承函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图继承的类的功能,但它返回 NullPointerException异常。任何人都可以请帮助?

这是我尝试在 Main.java 继承功能。此功能在 Main.java 运行良好。

 公共无效READDATA(){
    串readString =;    尝试{
        FIN的FileInputStream = openFileInput(user.txt);
        InputStreamReader的ISR =新的InputStreamReader(FIN);
        的BufferedReader buffreader =新的BufferedReader(ISR);        readString = buffreader.readLine();
        isr.close();    }赶上(IOException异常IOE){
        ioe.printStackTrace();
    }    telephonyManager.listen(myPhoneStateListener,
                            PhoneStateListener.LISTEN_CALL_STATE);
}

下面是我的另一个类的编码:

 主力主攻=新的Main();
main.readData();

错误日志:

 二月4日至16日:58:23.812:E / AndroidRuntime(8125):显示java.lang.NullPointerException
2月四日至16日:58:23.812:E / AndroidRuntime(8125):在android.content.ContextWrapper.openFileInput(ContextWrapper.java:167)
2月四日至16日:58:23.812:E / AndroidRuntime(8125):在com.test.Main.readData(Main.java:176)


解决方案

的问题是, openFileInput()需要上下文。您需要创建在您的类一个构造函数背景参数

 公共无效ClassWithFunction
{
   上下文mContext;
    公共ClassWithFunction(上下文的背景下)
    {
         mContext =上下文
    }
}

然后通过你的上下文

 主力主攻=新的主(本);
main.readData();

然后使用,当你调用这个函数。

 的FileInputStream FIN = mContext.openFileInput(user.txt); // mContext的背景下,从活动发送

注意
你会想用这个上下文如果你有这个类中的任何其他方法需要它

<一个href=\"http://stackoverflow.com/questions/16006486/try-catch-not-working-inside-a-custom-class-npe/16006611#16006611\">Here是我对SO的是同一件事的答案

I am trying to inherit a function from a class, but it returns NullPointerException. Can anyone please help?

This is the function I try to inherit in Main.java. This function operates well in Main.java.

public void readData() {
    String readString = "";

    try {
        FileInputStream fIn = openFileInput("user.txt");
        InputStreamReader isr = new InputStreamReader(fIn);
        BufferedReader buffreader = new BufferedReader(isr);

        readString = buffreader.readLine();
        isr.close();

    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    telephonyManager.listen(myPhoneStateListener,
                            PhoneStateListener.LISTEN_CALL_STATE);
}

Here is the coding in my another class:

Main main =new Main();
main.readData();

Error log:

04-16 02:58:23.812: E/AndroidRuntime(8125): java.lang.NullPointerException
04-16 02:58:23.812: E/AndroidRuntime(8125):   at android.content.ContextWrapper.openFileInput(ContextWrapper.java:167)
04-16 02:58:23.812: E/AndroidRuntime(8125):   at com.test.Main.readData(Main.java:176)

解决方案

The problem is that openFileInput() needs a Context. You need to create a constructor in your class that takes context as a param.

public void ClassWithFunction
{
   Context mContext;
    public ClassWithFunction (Context context)
    {
         mContext = context
    }
}

then pass your Context

Main main =new Main(this);
main.readData();

then use that when you call this function

FileInputStream fIn = mContext.openFileInput("user.txt");  //mContext is context sent from Activity

Note You will want to use this Context if you have any other methods in this class that need it

Here is an answer of mine on SO about the same thing

这篇关于NullPointerException异常从一个类继承函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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