The programme below is trying to take a single text from the first edit box and match the word vysh and produce the output in the second editbox .The second edit box has dots initially and if each word matches the word VYSH then it replaces the dots .
I get an error while running the programme and the error is the following:
09-14 19:47:25.593: D/AndroidRuntime(469): Shutting down VM
09-14 19:47:25.593: W/dalvikvm(469): threadid=1: thread exiting with uncaught exception (group=0x40014760)
09-14 19:47:25.593: E/AndroidRuntime(469): FATAL EXCEPTION: main
09-14 19:47:25.593: E/AndroidRuntime(469): java.lang.NullPointerException
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.String.<init>(String.java:505)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.example.testinput.MainActivity$1.onClick(MainActivity.java:46)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.view.View.performClick(View.java:3110)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.view.View$PerformClick.run(View.java:11934)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Handler.handleCallback(Handler.java:587)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Looper.loop(Looper.java:132)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.app.ActivityThread.main(ActivityThread.java:4123)
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.reflect.Method.invoke(Method.java:491)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-14 19:47:25.593: E/AndroidRuntime(469): at dalvik.system.NativeStart.main(Native Method)
09-14 19:47:30.175: I/Process(469): Sending signal. PID: 469 SIG: 9
The program is shown below:
package com.example.testinput;
public class MainActivity extends Activity {
String rword="vysh";
private StringBuffer gword=new StringBuffer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button1);
final EditText output=(EditText) findViewById(R.id.editText2);
//input dots in edittext
char positions[] = new char[rword.length()];
for (int i=0; i<rword.length(); i++) {
positions[i] = '.';
}
String dots=new String(positions);
output.setText(dots);
//end of input dots
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText text=(EditText)findViewById(R.id.editText1);
String str="";
str=text.getText().toString();
if(!TextUtils.isEmpty(str))
{
//String t=new String(gword);
char a=str.charAt(0);
//replace dots with text
for (int i=0; i<rword.length(); i++)
{
if((rword.charAt(i))==a)
{
gword.setCharAt( i, a);
}
}
//print the output
output.setText(gword);
}end of iftextutils
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}
}
Tried initializing stringbuffer and edited the program based on below solutioons but the program doesn’t work…I get the following error:09-17 10:48:11.169: 09-17 11:36:03.439: E/AndroidRuntime(926): java.lang.StringIndexOutOfBoundsException: index=0 length=0
09-17 11:36:03.439: E/AndroidRuntime(926): at java.lang.AbstractStringBuilder.indexAndLength(AbstractStringBuilder.java:214)
09-17 11:36:03.439: E/AndroidRuntime(926): at java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:544)
09-17 11:36:03.439: E/AndroidRuntime(926): at java.lang.StringBuffer.setCharAt(StringBuffer.java:698)
09-17 11:36:03.439: E/AndroidRuntime(926): at com.example.testinput.MainActivity$1.onClick(MainActivity.java:60)
09-17 11:36:03.439: E/AndroidRuntime(926): at android.view.View.performClick(View.java:3110)
It shows there is an error at String.charat function ..Basically i want to replace the dots in the editbox to characters in the first box if it matches the word vysh.
In the line
String t=new String(gword);, the variablegwordis null, which causes the Exception. As far as I can see, you’re not usingtanyways, so … remove the line, or createtwith a simplenew String().