为数据持久性保存数组会使用gson获取空指针异常

时间:2015-02-01 08:13:04

标签: java android nullpointerexception output

我不打算发布所有代码,因为有数百行,但是我会在LOGCAT中发布带有空指针异常的返回行,以及我用于Gson的LOADFROMFILE和SAVETOFILE方法序列化和数据存储。

这里的第一个错误点是:

Gson gson = new Gson();
try {
    FileOutputStream fos = openFileOutput(SAVEFILE,0); // This line is the error
    OutputStreamWriter osw = new OutputStreamWriter(fos);

第二点:

AddClaim cla = new AddClaim();
cla.saveInFile(claim); // This line is the error

第三点:

public void addClaim(Claim claim){
    getClaimList().addClaim(claim);

第四点:

this.edate = getDateFromDatePicket(edatePicker);
addClaims(v);

第五点/最后一点:

public void addClaims(View v){
    ClaimListController ct = new ClaimListController();
    Claim addClaim = new Claim(name, sdate, edate);
    ct.addClaim(addClaim);
    Toast.makeText(this,"Added "+name, Toast.LENGTH_SHORT).show();
}

现在在我看来,这些错误都有共同之处,我正在尝试添加新的声明。但是,当我删除文件持久性代码并且只是在没有存储的情况下运行它时,代码可以工作,并且声明显示在我的应用程序上。这必然意味着它不是通过null而是其他东西的声明吗?那是什么东西?

哦,以下是我用来保存和加载文件中数据的Add Claim中的两种方法:

作为我所拥有的课程的一个属性:

String SAVEFILE = "file.sav";

然后是其余代码,然后是两个加载和保存方法:

public void saveInFile(Claim claim) {
    Gson gson = new Gson();
    try {
        FileOutputStream fos = openFileOutput(SAVEFILE,0);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        gson.toJson(claim, osw);
        osw.flush();
        fos.close();
    }
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

// Recover those persistent data created in the save function
private ArrayList<Claim> loadFromFile() {
    Gson gson = new Gson();
    ArrayList<Claim> claim = new ArrayList<Claim>();
    try {
        FileInputStream fis = openFileInput(SAVEFILE);
        //Based on http://google.gson.googlecode.com/svn/trunk/gson/dos/javadoc/com/google/gson/Gson.html
        Type listType = new TypeToken<ArrayList<Claim>>(){}.getType();
        InputStreamReader isr = new InputStreamReader(fis);
        claim = gson.fromJson(isr, listType);
        fis.close();
    }
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (claim == null){
        claim = new ArrayList<Claim>();
    }
    return claim;
}

请让我知道我还需要什么。

Here is the LOGCAT

02-01 01:05:37.105: E/AndroidRuntime(23093): FATAL EXCEPTION: main
02-01 01:05:37.105: E/AndroidRuntime(23093): Process: app.zioueche_travelexpense, PID: 23093
02-01 01:05:37.105: E/AndroidRuntime(23093): java.lang.IllegalStateException: Could not execute method of the activity
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.view.View$1.onClick(View.java:3969)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.view.View.performClick(View.java:4633)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.view.View$PerformClick.run(View.java:19330)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.os.Handler.handleCallback(Handler.java:733)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.os.Looper.loop(Looper.java:157)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.app.ActivityThread.main(ActivityThread.java:5356)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at java.lang.reflect.Method.invoke(Method.java:515)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at dalvik.system.NativeStart.main(Native Method)
02-01 01:05:37.105: E/AndroidRuntime(23093): Caused by: java.lang.reflect.InvocationTargetException
02-01 01:05:37.105: E/AndroidRuntime(23093):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at java.lang.reflect.Method.invoke(Method.java:515)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.view.View$1.onClick(View.java:3964)
02-01 01:05:37.105: E/AndroidRuntime(23093):     ... 11 more
02-01 01:05:37.105: E/AndroidRuntime(23093): Caused by: java.lang.NullPointerException
02-01 01:05:37.105: E/AndroidRuntime(23093):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:197)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at app.zioueche_travelexpense.AddClaim.saveInFile(AddClaim.java:244)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at app.zioueche_travelexpense.ClaimsList.addClaim(ClaimsList.java:37)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at app.zioueche_travelexpense.ClaimListController.addClaim(ClaimListController.java:19)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at app.zioueche_travelexpense.NewClaim.addClaims(NewClaim.java:34)
02-01 01:05:37.105: E/AndroidRuntime(23093):     at app.zioueche_travelexpense.NewClaim.getEDate(NewClaim.java:68)

第244行以:

开头
try {
    FileOutputStream fos = openFileOutput(SAVEFILE,0);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    gson.toJson(claim, osw);
    osw.flush();
    fos.close();
}

1 个答案:

答案 0 :(得分:0)

在loadFromFile()方法中,在最顶部添加以下四行:

File myFile = new File(SAVEFILE);
System.out.println("Method 1, file exists?: " + myFile.exists());
File myFile2 = new File(getFilesDir(), "/" + SAVEFILE);
System.out.println("Method 2, file exists?: " + myFile2.exists());

如果两者都是假的,那么我就不知道答案了。 如果你在第二个时间变为真,那么你可能需要在查找文件时提供绝对路径。