• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

NullPointerException - Can someone help me regarding this?

 
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I'm wondering if someone could help me with this?

The full project can be found here https://github.com/lnewportTX/TourGuideApp


I'm trying to implement both a ListView on a Fragment within a ViewPager with TabLayout.  
I got the ListView working on the Activity page first.  Saved that.
Then I implemented the ViewPager with TabLayout and got that working with my Fragments.
I then tried to implement the ListView on one of the Fragments and I receive the following Fatal Exception



com.example.android.tourguideapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.android.tourguideapp, PID: 8419
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                                   at com.example.android.tourguideapp.BBQFragment.onCreateView(BBQFragment.java:43)



Line 43 is listView.SetAdapter(itemsAdapter) and I don't think it's null.  I did an if statement to verify that it's not null.  It seems like the issue is in my LocationAdapter constructor.  I put LOG statement with LocationAdapter and the one in the constructor printed by that's as far it went before the Null Exception occurred.












 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:I then tried to implement the ListView on one of the Fragments and I receive the following Fatal Exception


There are four items in the list to choose from and this seems to only happen when you click on the first element in the list "Food".
The other three elements seems to work fine, well they do not produce this error at least.

What version of Android are you targeting?

You may not be aware of this, but Kotlin has some improvements over Java and it is officially supported for Android development by Google.
You can have a project which has both Kotlin and Java source files, which is a good way to migrate away from Java and to learn Kotlin.
 
Lisa Austin
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:

Lisa Austin wrote:I then tried to implement the ListView on one of the Fragments and I receive the following Fatal Exception


There are four items in the list to choose from and this seems to only happen when you click on the first element in the list "Food".
The other three elements seems to work fine, well they do not produce this error at least.

What version of Android are you targeting?

You may not be aware of this, but Kotlin has some improvements over Java and it is officially supported for Android development by Google.
You can have a project which has both Kotlin and Java source files, which is a good way to migrate away from Java and to learn Kotlin.



Only Food is currently setup Fragment within a ViewPager with TabLayout.  Sorry, I should have mentioned that.  
Target Version is 27 and minSdkVersion is 15
I want to learn Kotlin but currently I'm being taught in Java so it's not yet an option for me.   Soon though!


android {
   compileSdkVersion 27
   defaultConfig {
       applicationId "com.example.android.tourguideapp"
       minSdkVersion 15
       targetSdkVersion 27
       versionCode 1
       versionName "1.0"
       testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }

 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an issue with these lines in FoodActivity.java:To be more precise with lines 27 and lines 28 commented out the program does not crash.
However with these two lines uncommitted the program does crash.
Therefore I suspect that line 26 does not return the adapter which you want to use.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking closer I found out that in BBQFragment.java this line:returns a null.
What about rootView? Is that null as well or does it have a valid value?
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with this line of code in BBQFragment.java Once you provide the correct value for inflater you will get the results that you want.
 
Lisa Austin
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:The problem is with this line of code in BBQFragment.java Once you provide the correct value for inflater you will get the results that you want.



Thank you so much for your help.
Could you give me a hint as to what / which value is wrong here?    We didn't really cover the details of inflater in the class I'm taking just told to use it.  However I'm going through documentation to try and understand it.  
 
Lisa Austin
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:The problem is with this line of code in BBQFragment.java Once you provide the correct value for inflater you will get the results that you want.




I'm thinking the issue is with the container value.  The class example originally only had the fragment inside of the activity.



And the example showed the container's XML was what contained the fragment ( in this case NumbersFragment() ).

My code doesn't specify anything to contain the fragment, I just want to display it.   Am I on the right track by any chance?  
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rootView value that you had/have does not have any child view elements named bbq_list_view.

Essentially this is what happens:
1) Android loads R.layout.locations_list on line 35.
2) On line 42 Android looks for an element named bbq_list_view in the locations_list view.
3) Android is unable to find that view, so Android returns a null.
4) Result you get a NullPointerException as you have experienced.

The solution is to have the rootView inflate the view which contains the child element named bbq_list_view.

It did take me a bit to get to the correct end result.
Here are the general debugging steps I used:
1) Try out the program to see if the produces the same error/result as you
2) From the user experience find out where the error seems to happen by commenting out some lines of code.
    - This lead me to the post where I mention the problem with FoodActivity.java
3) Narrow down the error even more by jumping down further into the code/classes which lead me to the post about "listView" return null.
4) Find the code which should have setup the listView to see how that was loaded.
5) Change the value of the rootView to load in a different view (the view with bbq_list_view).

I used the shortcut "logd" in Android Studio many times to print intermediate values.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:And the example showed the container's XML was what contained the fragment ( in this case NumbersFragment() ).

My code doesn't specify anything to contain the fragment, I just want to display it.   Am I on the right track by any chance?

To get past the NullPointerException you only need to change the value of rootView and nothing else. After that you get a "working" program.
It may not do exactly what you want, but what would then be a business logic thing. For instance you may need to add in an onClick event or something like that, but that is a different problem.
 
Lisa Austin
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:The rootView value that you had/have does not have any child view elements named bbq_list_view.

Essentially this is what happens:
1) Android loads R.layout.locations_list on line 35.
2) On line 42 Android looks for an element named bbq_list_view in the locations_list view.
3) Android is unable to find that view, so Android returns a null.
4) Result you get a NullPointerException as you have experienced.

The solution is to have the rootView inflate the view which contains the child element named bbq_list_view.

It did take me a bit to get to the correct end result.
Here are the general debugging steps I used:
1) Try out the program to see if the produces the same error/result as you
2) From the user experience find out where the error seems to happen by commenting out some lines of code.
    - This lead me to the post where I mention the problem with FoodActivity.java
3) Narrow down the error even more by jumping down further into the code/classes which lead me to the post about "listView" return null.
4) Find the code which should have setup the listView to see how that was loaded.
5) Change the value of the rootView to load in a different view (the view with bbq_list_view).

I used the shortcut "logd" in Android Studio many times to print intermediate values.



Oh that makes sense. Now I can see that.
Thank you so much for this and for explaining how you figured it out.  Figuring out what the issue is , is always a challenge for me.
I also see that there seems to be an issue with my tabs but I think I can figure this out.   Thank You so much!  

 
Lisa Austin
Ranch Hand
Posts: 331
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:

Lisa Austin wrote:And the example showed the container's XML was what contained the fragment ( in this case NumbersFragment() ).

My code doesn't specify anything to contain the fragment, I just want to display it.   Am I on the right track by any chance?

To get past the NullPointerException you only need to change the value of rootView and nothing else. After that you get a "working" program.
It may not do exactly what you want, but what would then be a business logic thing. For instance you may need to add in an onClick event or something like that, but that is a different problem.



Yea I see my tabs aren't tabbing exactly how I thought it should but I think I can figure this part out.  Thank you again so much for explaining it all to me.  I also learned more about this inflator part as well trying to figure things out.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are most welcome.
I glad that you got it working and I'm glad I was able to help you out.
 
I am going to test your electrical conductivity with this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic