creating and writing to file

Put your problem here if it does not fit any of the other categories.

creating and writing to file

Postby prolink007 » Sat Mar 26, 2011 6:06 am

I am trying to create a file with my application and write to that file. What i have so far is listed below. I keep getting a nullpointerexception whenever the application attempts to write to the file. "writer" is currently null at that point. It almost seems like onCreate() is not being called. Any help will be greatly appreciated!!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // this is the class where i am attempting to create the file and write to it
  2. public class DataRobot extends Activity {
  3.         @Override
  4.         public void onCreate(Bundle savedInstanceState) {
  5.  
  6.         private FileOutputStream fOut;
  7.         private OutputStreamWriter writer;
  8.  
  9.                 super.onCreate(savedInstanceState);
  10.                 try {
  11.                         //file = new File("/sdcard", "test.csv");
  12.                         fOut = openFileOutput("test.csv", MODE_WORLD_READABLE);
  13.                         writer = new OutputStreamWriter(fOut);
  14.                 } catch (NullPointerException e) {
  15.                         e.printStackTrace();
  16.                 } catch (FileNotFoundException e) {
  17.                         e.printStackTrace();
  18.                 } catch (IOException e) {
  19.                         e.printStackTrace();
  20.                 }
  21.         }
  22.         public void analyzeData(SmartDataObject temp) {
  23.                 try {
  24.                        // this is where the error is occurring
  25.                         writer.write(Double.toString(data.getHeartRate()));
  26.                 } catch (IOException e) {
  27.                         // TODO Auto-generated catch block
  28.                         e.printStackTrace();
  29.                 }
  30.         }
  31. }
  32.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // this is the error i get everytime at that point in the code i labeled.
  2. 03-26 04:48:30.938: WARN/System.err(278): java.lang.NullPointerException
  3. 03-26 04:48:30.957: WARN/System.err(278):     at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:81)
  4. 03-26 04:48:30.989: WARN/System.err(278):     at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49)
  5. 03-26 04:48:31.018: WARN/System.err(278):     at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
  6. 03-26 04:48:31.018: WARN/System.err(278):     at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
  7. 03-26 04:48:31.038: WARN/System.err(278):     at java.lang.Thread.run(Thread.java:1096)
  8.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
prolink007
Freshman
Freshman
 
Posts: 2
Joined: Sat Mar 26, 2011 5:58 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests