Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Viewmodel null after initializing

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after initializing the ViewModel i keep getting an error of null object reference , and i don't know why













and this is the exception am getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
       at com.example.foodapp.RecipeActivity$1.onChanged(RecipeActivity.java:63)
       at com.example.foodapp.RecipeActivity$1.onChanged(RecipeActivity.java:57)
       at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
       at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
       at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
       at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)..etc

the model class :





I've tried cleaning the project and invalidate and cache and even using the deprecated ViewModelProviders class ,

but all the same and nothing has changed
 
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the getRecipeId method returning a null value?
Where is the code that creates a new instance of the Recipe class?  Does it provide a non null value for id?

Please edit your post and wrap each section of code in code tags: Select the code and press the Code button,
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the code for Recipe instance :

 






when the user clicks on the list of recipes (which are in another activity) an intent is send to the RecipeActivity with the selected recipe instance.

yes getRecipeId is returning a null value , while it should not.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the code that creates a new instance of the Recipe class?  
Does it provide a non null value for id?
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no such code for creating an instance of the Recipe class , Recipe instance is a model object for the data that am getting through an API using retrofit , when i want a particular recipe details i click an item in my Recipes list and i use the recipe id that I've clicked to get the entire details , those details are obtained via the intent that opens another activity (RecipeActivity)

my previous reply is where the recipe instance is being assigned a value .

i used a breakpoint on the code line(      mRecipeViewModel.searchRecipeById(recipe.getRecipeId()); (line 49)) in which i got a value for the id;

but when i did another breakpoint on line (  if(recipe.getRecipeId().equals(mRecipeViewModel.getViewModelRecipeId())) line (59) the id was null
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you add some debug code to the Recipe class that prints out the value of id when the constructor is called and when the the setRecipeId method is called to see if id ever has a valid value?

Your debugging shows that sometimes id has a valid value and sometimes it is null.  You need to fix the case for it being null.
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please help me to write such code , am new to the android programming
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have posted the same contents for your last two posts.  You might have missed my post.
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did not miss it , but am new to the website am trying to find my way around it , i asked for help for such debugging code , because am kinda new to android programming .
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

help for such debugging code ,


I use print statements for debugging:

The printed lines will be in the logcat.
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The logcat showed me that the id in the constructor is 0.
it did not print out the setRecipeId().
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that print out in the logcat shows that only one instance of the Recipe class was created and that the value for id was "0".
Does that make sense?  Is that the correct id value?
 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it dose not make sense , and of course that's not the correct value for the id . help please    


in the displaySearchCategory() i create an instance of the recipe, maybe that's why i have to different id values?
 
this is in the adapter class

 
lina saeed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the whole adapter class
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The print out shows that only ONE call was made to the Recipe class's constructor.    How many instances of Recipe are created?
If there are other instances of the Recipe class that were created without calling the constructor how were they created?  
Where does the data come from that fills the other instances?


Look in the displaySearchCategories() method.  It creates Recipes without using the  constructor shown in the posted code and without setting the id.
Change that method to make sure the id is being set.
 
Ranch Hand
Posts: 546
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem should be that the bundle is not passed. in other words you are launching the class proper activity with a so called explicit intent without passing  a proper parameter. Please see how explicit intent are handled passing data via Bundle!
 
Saloon Keeper
Posts: 24825
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

help for such debugging code ,


I use print statements for debugging:

The printed lines will be in the logcat.



Why not just use the Android logging facility?

A) you can switch it on and off without deleting/inserting statements (many a module has been damaged in the process of changing print statements)

B) If it was worth noting at one time, there may be a future time when it's worth reporting again. Here, too, you can get that information just by flipping a (loglevel) switch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic