致命异常:主要和Nullpointer

时间:2012-07-10 13:48:46

标签: java android

我在几天内遇到了这个非常烦人的问题,我似乎无法解决它。我正在尝试打开.csv文件,因此我将其导入到项目的res/raw/文件夹中。然后我试图通过getResources()方法打开并阅读它,这正是我得到NullPointerException的地方。这是读取文件并使用可用行填充数组的方法。

这是我的Activity,我在课程newwords中创建了一个对象Words,然后我想从类PlayWithRawFiles()调用Words方法。

public class Swear_Activity extends Activity implements OnInitListener, OnClickListener {

    private Words newwords;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swear);
    }
    public void onClick(View view){
        try {
            newwords.PlayWithRawFiles();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("greshka");
            e.printStackTrace();
        }
    }
}

现在这是我收到错误的类

public class Words{

    public Word[] wordsArray;
    private String locale = "de";

    public Words(String locale) {
        if (locale != null ) {
            this.locale = locale;
        }
    }

    Context c;
    public void PlayWithRawFiles() throws IOException  {
        String str="";
        StringBuffer buf = new StringBuffer();          
        int i = 0;
        InputStream is = c.getResources().openRawResource(R.raw.est);
        BufferedReader reader = null;
        try{
            if (is != null) reader = new BufferedReader(new InputStreamReader(is));
        }
        catch(Exception e) {
            System.out.println ("ss");
            e.printStackTrace();
        }

        if (is!=null) {                         
            while ((str = reader.readLine()) != null) { 
                Word wd = new Word(1,"str");
                this.wordsArray[i] = wd;
                i++;
            }               
        }       
        is.close();
    }
}

这是Word类

public class Word {
    private int type;
    private String data;

    public Word(int type, String data){
        this.type = type;
        this.data=data;
    }

    public int getType(){
        return this.type;
    }
    public String getData(){
        return this.data;
    }
}

这是堆栈跟踪

  

07-10 13:28:58.558:E / AndroidRuntime(647):致命异常:主

     

07-10 13:28:58.558:E / AndroidRuntime(647):java.lang.NullPointerException

     

07-10 13:28:58.558:E / AndroidRuntime(647):at de.android.swearapp.Swear_Activity.onClick(Swear_Activity.java:32)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at> android.view.View.performClick(View.java:2408)

     

07-10 13:28:58.558:E / AndroidRuntime(647):在android.view.View $ PerformClick.run(View.java:8816)

     

07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Handler.handleCallback(Handler.java:587)

     

07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Handler.dispatchMessage(Handler.java:92)

     

07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Looper.loop(Looper.java:123)

     

07-10 13:28:58.558:E / AndroidRuntime(647):在android.app.ActivityThread.main(ActivityThread.java:4627)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at java.lang.reflect.Method.invokeNative(Native Method)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at java.lang.reflect.Method.invoke(Method.java:521)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

     

07-10 13:28:58.558:E / AndroidRuntime(647):at dalvik.system.NativeStart.main(Native Method)

我做错了什么?我真的被这个困住了。提前谢谢!

3 个答案:

答案 0 :(得分:2)

你从未实例化过newwords对象。

您需要在onClick

之前在onCreate中添加类似的内容
newwords = new Words("");

答案 1 :(得分:0)

我认为问题在于你没有给Context c(在Words中)一个实际值,所以c.getResources()会抛出一个NullPointerException。

答案 2 :(得分:0)

我在这里看到两件事,

  • 您没有像newwords;
  • 那样实例化newwords = new Words("")
  • c为空,未分配上下文。

我建议你像这样重新编写类:

public class Words{
   public Word[] wordsArray;
   private String locale = "de";
   private Context c;
   public Words(String locale, Context paramContext) {
      if (locale != null ) {
         this.locale = locale;
      }
      c = paramContext;
   }
// Rest of code ok.
//
// Remove the Context c variable further on as it is declared above!
}

所做的是,我们只是将Context添加为类Words的构造函数的一部分。然后从Words类:

实例化类SwearActivity
public void onClick(View view){
    newwords = new Words("", getApplicationContext());
    try {
        newwords.PlayWithRawFiles();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("greshka");
        e.printStackTrace();
    }
}