Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to display array in textview but when rum the APP have this message "unfortunately has stopped"

What I have tried:

This is code
Java code
Java
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       String[][] a = new String[][]{
               {"000","aaaa"},
               {"111","nnnn"},
               {"222","ssss"},
               {"333","zaaaa"},
               {"444","ahhhh"},
               {"555","mmmmmm"}
                       };

       tv=(TextView)findViewById(R.id.textView1);
       for(int i=0 ; i<a.length;i++){>
            for(int j=0 ; j<a.length;j++){>
           tv.setText(a[i][j]);

            }
       }

   }


This is Logcat
05-04 17:46:36.387: D/AndroidRuntime(1725): Shutting down VM
05-04 17:46:36.387: W/dalvikvm(1725): threadid=1: thread exiting with uncaught exception (group=0x94cbdb20)
05-04 17:46:36.399: E/AndroidRuntime(1725): FATAL EXCEPTION: main
05-04 17:46:36.399: E/AndroidRuntime(1725): Process: com.example.contacts, PID: 1725
05-04 17:46:36.399: E/AndroidRuntime(1725): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contacts/com.example.contacts.MainActivity}: java.lang.NullPointerException
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.os.Handler.dispatchMessage(Handler.java:102)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.os.Looper.loop(Looper.java:136)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread.main(ActivityThread.java:5045)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at java.lang.reflect.Method.invokeNative(Native Method)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at java.lang.reflect.Method.invoke(Method.java:515)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at dalvik.system.NativeStart.main(Native Method)
05-04 17:46:36.399: E/AndroidRuntime(1725): Caused by: java.lang.NullPointerException
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at com.example.contacts.MainActivity.onCreate(MainActivity.java:32)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.Activity.performCreate(Activity.java:5231)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
05-04 17:46:36.399: E/AndroidRuntime(1725): 	... 11 more
05-04 17:51:36.747: I/Process(1725): Sending signal. PID: 1725 SIG: 9
Posted
Updated 4-May-16 1:40am
v2
Comments
Mohibur Rashid 4-May-16 6:56am    
Your for loop is incorrect and pointless.
First of all
for(int i=0 ; i<a.length;i++){>
for(int j=0 ; j<a.length;j++){>
tv.setText(a[i][j]);

}
}

tv.setText(a[i][j]);

would be :
tv.setText(a[0][0]);
tv.setText(a[0][1]);
tv.setText(a[0][2]);//this is where your application failed.


and putting value in for loop in the same text box will result
"mmmmmm"

May be rethink your algorithm
_Tuba 4-May-16 7:17am    
It's the same error :( and I want to add other element to array and then show it,if I have a[2][100] how can do this?? :)
Mohibur Rashid 4-May-16 9:56am    
if you need more clue
i will be from 0 to 5
j will be from 0 to 1

1 solution

As noted by Mohibur Rashid your loops are wrong (and cause the exception) and the logic of your program is flawed: it makes no sense to iteratively call setText method. You know, you can show just one string in the text box, hence you can show just one array item (or multiple ones if you concatenate their values in a single string).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900