• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Attempt to invoke virtual method "" on a null object reference

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I am coding a physics based app in android studios and I keep getting this error,  "Attempt to invoke virtual method ""  on a null object reference" This is the code I have made for it.



I have no idea why I am getting this issue because it is working in my other classes perfectly fine.
 
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you getting that warning? Is that something from an IDE? It isn't a standard compiler error, and it wouldn't be shown at runtime because calling a method on a reference to null would cause an exception to be thrown. Why does it say, “virtual” when there is no such thing as a virtual method in Java┬«?
Please tell us which reference seems to point to null.
You have all sorts of other style problems, e.g. variable names starting with Capital Letters, and the multiple dot operators which suggest you aren't marking your fields private.
You don't need the negation operator in line 17; you can use subtraction instead. You probably don't need toString() on something that is text already.
 
Zain Toor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the full error I am receiving "Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference", in terms of other errors all of that is fine.
 
Saloon Keeper
Posts: 13886
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do InitialVelocity, FinalVelocity and timeTotal really point to things in your view? Have you checked for typos and capitalization?
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zain Toor wrote:. . . virtual method 'android.text.Editable android.widget.EditText.getText()' . . .

Why didn't you say it was Android? Android isn't Java®, so those style things are probably all correct.
I shall move you to our Android forum.
But which reference does it say is null? Or a line number please.

Please don't quote the whole of a preceding post; that adds nothing new and is liable to be removed.
 
Zain Toor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, sorry my bad. It is this line that is giving me the error.
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So which is null? V1 or V1.getText()?
 
Zain Toor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:So which is null? V1 or V1.getText()?



I believe it is appearing in the V1.getText(). I apologize if that is incorrect.
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to check programmatically which is null.
 
Zain Toor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You need to check programmatically which is null.



Ok I ran it and the issue is with the getText
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you have a component with null for its text. You should have arranged to set the text attribute earlier.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic