0

I want to print all these numbers with the help of an array, not ArrayList...

Showing this error message:

2021-04-11 12:32:51.142 8075-8075/com.example.listviewtest E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.listviewtest, PID: 8075 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewtest/com.example.listviewtest.UsingArray}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7560) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.listviewtest.UsingArray.onCreate(UsingArray.java:58) at android.app.Activity.performCreate(Activity.java:7894) at android.app.Activity.performCreate(Activity.java:7881) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)

Here it's the code

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Arrays;

public class UsingArray extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_using_array);
        String[] numbers = new String[20];
        numbers[0] = ("One");
        numbers[1] = ("Two");
        numbers[2] = ("Three");
        numbers[3] = ("Four");
        numbers[4] = ("Five");
        numbers[5] = ("Six");
        numbers[6] = ("Seven");
        numbers[7] = ("Eight");
        numbers[8] = ("Nine");
        numbers[9] = ("Ten");
        numbers[10] = ("Eleven");
        numbers[11] = ("Twelve");
        numbers[12] = ("Thirteen");
        numbers[13] = ("Fourteen");
        numbers[14] = ("Fifteen");
        numbers[15] = ("Sixteen");
        numbers[16] = ("Seventeen");
        numbers[17] = ("Eighteen");
        numbers[18] = ("Nineteen");
        numbers[19] = ("Twenty");

//        LinearLayout arrayTextView = (LinearLayout) findViewById(R.id.arrayTextView);

//        Log.v("UsingArray", "In index 0 = " + numbers[0]);
//        Log.v("UsingArray", "In index 1 = " + numbers[1]);
//        Log.v("UsingArray", "In index 2 = " + numbers[2]);
//        Log.v("UsingArray", "In index 3 = " + numbers[3]);
//        Log.v("UsingArray", "In index 4 = " + numbers[4]);
//        Log.v("UsingArray", "In index 5 = " + numbers[5]);
//        Log.v("UsingArray", "In index 6 = " + numbers[6]);
//        Log.v("UsingArray", "In index 7 = " + numbers[7]);
//        Log.v("UsingArray", "In index 8 = " + numbers[8]);
//        Log.v("UsingArray", "In index 9 = " + numbers[9]);
//        Log.v("UsingArray", "In index 10 = " + numbers[10]);
        TextView textView = (TextView) findViewById(R.id.usingArrayTextView);
//        for (int y = 0; y <= numbers.length - 1; y++) {
//            textView.setText(Arrays.toString(new String[]{numbers[y]}));
//        }
        textView.setText(Arrays.toString(numbers));
    }
}
Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32

1 Answers1

0

There are two ways of doing it :

The below java code works fine in my case :

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Arrays;

public class UsingArray extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_using_array);
    String[] numbers = new String[20];
    numbers[0] = ("One");
    numbers[1] = ("Two");
    numbers[2] = ("Three");
    numbers[3] = ("Four");
    numbers[4] = ("Five");
    numbers[5] = ("Six");
    numbers[6] = ("Seven");
    numbers[7] = ("Eight");
    numbers[8] = ("Nine");
    numbers[9] = ("Ten");
    numbers[10] = ("Eleven");
    numbers[11] = ("Twelve");
    numbers[12] = ("Thirteen");
    numbers[13] = ("Fourteen");
    numbers[14] = ("Fifteen");
    numbers[15] = ("Sixteen");
    numbers[16] = ("Seventeen");
    numbers[17] = ("Eighteen");
    numbers[18] = ("Nineteen");
    numbers[19] = ("Twenty");

    TextView textView = (TextView) findViewById(R.id.usingArrayTextView);
    textView.setText(Arrays.toString(numbers));
}

}

Layout File

The layout file

and the output is fine I didn't get any null pointer exception

Output Image Feel free to ask if something is unclear.

Kamal Nayan
  • 1,635
  • 1
  • 5
  • 19