Android アプリケーションでファイルを作成しようとしています。特定のクラスのファイルに書き込む必要があります。私が現在持っているコードを以下に示します。ファイルに書き込もnullpointerexception
うとしているときに、私は取得し続けます。正確なエラーを以下に示します。Android初心者なので詳しく教えてください。
誰かが私が間違っていることと、この問題を解決する方法を教えてもらえますか?
//the class where i need to create and write to the file
public class DataRobot {
Context tThis;
FileOutputStream fOut;
OutputStreamWriter writer;
public DataRobot(SmartApp smartApp) extends Activity {
tThis = (Context) smartApp;
}
public void onCreate(Bundle savedInstanceState) {
try {
//file = new File("/sdcard", "test.csv");
fOut = openFileOutput("test.csv", MODE_WORLD_READABLE);
writer = new OutputStreamWriter(fOut);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void analyzeData(SmartDataObject temp) {
data = temp;
try {
//the following line is where the error is occurring.
writer.write(Double.toString(data.getHeartRate()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//the error
03-26 03:47:53.924: WARN/System.err(273): java.lang.NullPointerException
03-26 03:47:53.934: WARN/System.err(273): at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:80)
03-26 03:47:53.946: WARN/System.err(273): at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49)
03-26 03:47:53.956: WARN/System.err(273): at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
03-26 03:47:53.956: WARN/System.err(273): at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
03-26 03:47:53.965: WARN/System.err(273): at java.lang.Thread.run(Thread.java:1096)