-1

I am trying to get name of the TextView object which is not yet Initialized.

In MainActivity class

TextView =varTextView1,varTextView2,varTextView3;
TextView textViewArray [];

In function Array Init ;-

 textViewArray = new TextView[] {varTextView1,varTextView2,varTextView3};

In another function :-

String s;
s = textViewArray[0].toString();

Tried with

.getTransitionName();

Error:-

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.TextView.toString()' on a null object reference

No Error on compile but on Run-time.
Searched on the net But no luck
Can anyone please help me.

jesugmz
  • 2,320
  • 2
  • 18
  • 33
sandy560
  • 49
  • 1
  • 8
  • 1
    https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510 – Dave S Aug 13 '18 at 23:07
  • @dave But here after referencing I'm using it. – sandy560 Aug 13 '18 at 23:11
  • @dave Also now i tried with 'TextView textViewArray [] = new TextView[] {varTextView1,varTextView2,varTextView3}' .Still getting the same error.could you please help...... – sandy560 Aug 13 '18 at 23:19
  • 1
    That's the problem, you're referencing a null pointer. That's why you get a Null Pointer Exception. You need to wait until it's initialized before you reference it – Dave S Aug 13 '18 at 23:32
  • Ok i got your point actually I'm just Initializing the array but not the object inside it.thnx.... – sandy560 Aug 13 '18 at 23:39

1 Answers1

0

The variables of the type TextView that you created in the first line of the posted code are just that, variables of the type TextView. They aren't actual objects. In Java, a variable of a certain class type can reference instances (objects) of that class, but they are not the object themselves.

In this context, since you didn't specify an object for your variables of the type TextView to reference to (creating, for example, a new one with the "new" operator, like new TextView()) a java.lang.NullPointerException was thrown, meaning that you tried to call a method on a null object reference.

I hope my explanation helps you. See ya!

Talendar
  • 1,841
  • 14
  • 23