• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 28193
95
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic