• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ERROR/AndroidRuntime(706): java.lang.RuntimeException: Unable to start activity

Hello everybody, I have write one application that writes data to the text file, but when I run application it throws following exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micro/com.micro.MyProject}: java.lang.NullPointerException

what's wrong in my code.

MyProject.java file contains following code

Code:
 [LIST=1]
[*][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]package[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] com.micro;
[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.app.Activity;[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] java.io.*;[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.content.Context;[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.os.Bundle;[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.widget.Toast;
[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MyProject [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity
{
Context con;

[/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/**[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]Called[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]when[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]the[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]activity[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]is[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]first[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]created.[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]*/[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Override
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onCreate(Bundle savedInstanceState) {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
setContentView(R.layout.main);
WriteSettings([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Pramod Deore"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WriteSettings(Context context, String data)
{ 
FileOutputStream fOut = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; 
OutputStreamWriter osw = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; 

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/LEFT]
[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{ 
fOut = openFileOutput([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"settings.txt"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],MODE_PRIVATE); 
osw = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OutputStreamWriter(fOut); 
osw.write(data); 
osw.flush(); 
Toast.makeText(context, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Settings saved"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],Toast.LENGTH_SHORT).show(); 
} 
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Exception e)
{ 
e.printStackTrace(); 
Toast.makeText([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Settings not saved"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],Toast.LENGTH_SHORT).show();

} 
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]finally[/LEFT]
[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{ 
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/LEFT]
[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{ 
osw.close(); 
fOut.close(); 
}
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (IOException e)
{ 
e.printStackTrace(); 
} 
} 
}[/LEFT]
}
[/SIZE]
[/LIST]and I have .txt file under src/com/micro/setting.txt file.
following is error part from Logcat file.



Code:
[LIST=1]
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.112[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): Uncaught handler: thread main exiting due to uncaught exception
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micro/com.micro.MyProject}: java.lang.NullPointerException
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:[COLOR=#c00000]2268[/COLOR])
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:[COLOR=#c00000]2284[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.access$[COLOR=#c00000]1800[/COLOR](ActivityThread.java:[COLOR=#c00000]112[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:[COLOR=#c00000]1692[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.os.Handler.dispatchMessage(Handler.java:[COLOR=#c00000]99[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.os.Looper.loop(Looper.java:[COLOR=#c00000]123[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.main(ActivityThread.java:[COLOR=#c00000]3948[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at java.lang.reflect.Method.invokeNative(Native Method)   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at java.lang.reflect.Method.invoke(Method.java:[COLOR=#c00000]521[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:[COLOR=#c00000]782[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:[COLOR=#c00000]540[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at dalvik.system.NativeStart.main(Native Method)   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): Caused by: java.lang.NullPointerException   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.widget.Toast.<init>(Toast.java:[COLOR=#c00000]88[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.widget.Toast.makeText(Toast.java:[COLOR=#c00000]230[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.micro.MyProject.WriteSettings(MyProject.java:[COLOR=#c00000]33[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.micro.MyProject.onCreate(MyProject.java:[COLOR=#c00000]18[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:[COLOR=#c00000]1123[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:[COLOR=#c00000]2231[/COLOR])   </SPAN>
[*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     ... [COLOR=#c00000]11[/COLOR] more   </SPAN>
[*]
[/LIST]</SPAN>

I also tried
WriteSettings(this, "Pramod Deore");

instead of
  1. WriteSettings(con, "Pramod Deore");
but still getting the same error.
Please help me how to solve this problem.

Thanks
 
Hello Everybody, now I think data is stored under
data/data/com/micro/Settings.txt file ,(because Now size of Settings.txt is increased,)but I don't understand how to see content of this file. And when I will install this application under phone then where I will get this file.

Use the DDMS in eclipse to find the file and you can then save it as a text file to somewhere on your machine and open it with a text editor
 
Upvote 0
Try this:

Code:
public void WriteSetting(final String fileName, final String fileText) {
		try {
		    File root = Environment.getExternalStorageDirectory();

		    if (root.canWrite()){
		        File theFile = new File(root, fileName);
		        FileWriter fileWriter = new FileWriter(theFile );
		        BufferedWriter out = new BufferedWriter(fileWriter);
		        out.write(fileText);
		        out.close();
		    }
		} catch (IOException e) {
			e.printStackTrace();
		}
}

This will write a file with 'fileName' and 'fileText' to the root of the SD Card.

In your manifest, you also need to declare:
HTML:
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones