• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Comparing Text Attributes of TextViews to text in a String

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spent the best part of a couple of days chasing this one round in circles.  Here's the scenario:
There are 12 TextViews, each has been assigned a single letter as the Text attribute.
There is a String containing up to 5 letters.

The requirement is to go through the TextViews, compare the Text to any of the letters in the String.  If there is a match, change the background color of that TextView.  Continue until all TextViews have been checked.

I've tried many and various ways of doing this   I think the problem might lie with the getText method of TextView and I have fiddled around trying to convert to and from CharSequence and String but without success.

The code below demonstrates just a very few of the things I've tried (just so you know I'm not asking without first having a good go at it!):



Tried this (which in theory I understood would compare a String with a CharSequence):


And this, in case I was wrong about the CharSequence comparison:

Plus many other combinations!

I've also tried building up an array and arraylist of Strings pulled from the TextViews in the kind of loops shown above and comparing strings with strings.  

All the above examples will compile in Android Studio, but all crash the app when it hits the getText line.

I'd be grateful for any suggestions for achieving what I thought (hah!) was going to be an easy task.

 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a TextView? If it is an Android class, I shall move you to the Android forum.
I can think of all sorts of ways to match them; I think I would consider a Map mapping the letter to the text view reference. Do you know how to use a Map?
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by crash? Please always tell us the full details so we can actually help.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What's a TextView? If it is an Android class, I shall move you to the Android forum.
I can think of all sorts of ways to match them; I think I would consider a Map mapping the letter to the text view reference. Do you know how to use a Map?



Yep I guess it must must be an Android element if you say you've not heard of it.

Thank you for moving it to the Android forum.  I will know in future to post there.  I;m a new member here and didn't know.

I will research Map and post back if that still doesn't solve it.


 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What do you mean by crash? Please always tell us the full details so we can actually help.



When I run the app and reach the point where the code does not work, the app stops running and closes.  Both on emulator and device.   No error messages, just closes.
Isn't that what "crash" normally means?



As a complete side issue and not wanting to get into a fight over this, I spent nearly 20 years moderating a VB forums site where we too had a tag line of "No question is a dumb question" and prided ourselves on our friendly approach.   It was "A friendly place for programming greenhorns" indeed.
If I wanted to get snapped at (twice on the same request for help) I'd have gone to StackOverflow.  I had kinda hoped for a friendlier response here.  #askingtoomuch?
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ged Mead wrote:. . . Isn't that what "crash" normally means? . . .

Most Java® applications throw an exception and there would be error messages.

. . . I'd have gone to StackOverflow. . . . friendlier response . . .

Sorry.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re Exception Messages.  I didn't get any automatically.   That said, I didn't include an Exception Handler in the code, so I probably should have tried that.  And will.

I did insert a  debug Log (log.d) line and ran it again.  The logged error comes out as:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
       at com.triviaconnexion.wordaround.MainActivity.QuickTest(MainActivity.java:283)

If this indicates that the null object reference is for the TextView objects,  I don't understand how this could be.  The TextView elements exist in the app and (I thought) they would exist in the array I created to hold them.
But perhaps that's not what is meant by null object reference in this case?

I had previously tried casting each item in that for loop specifically to a TextView, but that didn't work.

Any thoughts?
 
Saloon Keeper
Posts: 5868
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ged Mead wrote:
When I run the app and reach the point where the code does not work, the app stops running and closes.  Both on emulator and device.   No error messages, just closes.
?


In that situation the full stack trace of the exception should be in the logcat. Post the full stack trace here, it points to the exact line of code where the app crashed, and includes a message that points to what went wrong.
 
Tim Moores
Saloon Keeper
Posts: 5868
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there's the stack trace :-)

It sounds indeed as if the reference to the Textview is null. It's hard to say why that might be without seeing the code, and 283 sounds like the class might be a big one. Can you post a relevant excerpt of it, and indicate which one is line 283?
 
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An approach I use with NPE is to add a print statement just before the line where the NPE happens that prints out the contents of all the variables used on the line where the NPE happens.  The print out will show in the logcat.

The TextView elements exist in the app and (I thought) they would exist in the array I created


Do those variables have correct values when they are added to the array?
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:And there's the stack trace :-)

It sounds indeed as if the reference to the Textview is null. It's hard to say why that might be without seeing the code, and 283 sounds like the class might be a big one. Can you post a relevant excerpt of it, and indicate which one is line 283?



It's actually the MainActivity.java file.  It's a fairly large project already, Tim, using MVVM and various other utility classes that all feed into mainactivity.  I tried to pull out the key parts into a smaller project to upload but it's a bit tricky.

I'll try some more things first and if I'm still stuck will go back to creating that smaller project sample.  
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try printing the variables' values to find where the null value is?
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:An approach I use with NPE is to add a print statement just before the line where the NPE happens that prints out the contents of all the variables used on the line where the NPE happens.  The print out will show in the logcat.

The TextView elements exist in the app and (I thought) they would exist in the array I created


Do those variables have correct values when they are added to the array?



Thanks for that print suggestion.  

Because the variables in the array represent UI elements that 'physically' exist, I guess they must qualify as correct values.  

I'm experimenting with testing for !=null to see if I can trap it that way.  So far, it has passed that test and I haven't had the NPE, although I've still got a bit more testing to do.    I don't know enough about java to know if testing for null in this way is a good thing to do.

 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you try printing the variables' values to find where the null value is?



Sorry - I was still writing my reply to your suggestion when you followed up, so my last post might sound like a weird response!

TBH, I struggled to find the correct syntax for your print idea.  (New to java, coming from a VB background, it's one heck of a learning curve!).  I'd tried println and system.out.print, but both those gave me designtime errors.  I'm now trying to do it using Log.d -  as well as experimenting with !=null approach mentioned above.
 
Norm Radder
Rancher
Posts: 3523
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 I struggled to find the correct syntax for your print idea.  


Something like this:
for an array:
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

 I struggled to find the correct syntax for your print idea.  


Something like this:
for an array:



Thanks, Norm.    That's working fine now.  I'll plough (plow!) through and test each of the troublesome lines.
Hopefully that'll help pin it down.

 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I finally found it -  by using Norm's suggestion and working through each iteration until finally realising that the issue wasn't strictly the code in the loop, not exactly.

I had created an array of TextViews and passed in  the names of the 12 TextViews used in the UI.   BUT I'd failed to instantiate TextView #12, so of course the NPE occurred each time I reached that final item in the array because it contained an uninstantiated (if there is such a word) TextView.

Thanks for the help, guys.   Norm for the println suggestion and Tim also - if I'd not been so lazy and had pulled out and posted up the relevant code you'd probably have spotted my rookie mistake in an instant!
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!