Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring 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:
  • 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:

What to do about NullPointerException when trying to grab RelativeLayout and add views to it?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to grab the RelativeLayout that's there by default and add one single view to it - an AdView. I'm making a game and otherwise don't deal with Views. This is my code:



It's throwing a NullPointerException as soon as I attempt to access the rLayout. It says:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
 
Marshal
Posts: 27582
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The message is even clearer than NullPointerException messages given by Java: It clearly says that rLayout is null.

And your code assigns the result of the findViewById method to rLayout, so clearly that result is null. So you should look into why app.findViewById() is returning null. Without knowing anything about your application I would guess that the method is looking up views based on an ID, and the ID you passed to the method doesn't have any corresponding view.
 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The R.id.relativeLayout is referring to the only RelativeLayout in the xml file. Here is my xml file:



I don't understand why it's returning null.
 
I like tacos! And 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