Results 1 to 6 of 6
Thread: Null pointer exception
- 03-16-2010, 01:20 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
[SOLVED] Null pointer exception
Basically, my code is like this:
It compiles, but I get a run time error saying there's a null pointer exception where I've put the **. I'm not really sure what the problem is, I've initialised button1 and my array list so neither are null, and I've used an if statement so it only adds to array list if button1 is NOT null. i've tried initializing button1 to null, nothing seems to be working.Java Code:public class Viewer() { private ArrayList<Button> buttonsList; private Button button1; public Viewer() { buttonsList = new ArrayList<Button>(); addButtons(); } public void addButtons() { //other code emitted button1 = new Button("title"); if (button1 != null) { buttonsList.add(button1); ** } }
Any suggestions would be much appreciated!Last edited by izzy; 03-17-2010 at 01:59 PM. Reason: added code tags
- 03-16-2010, 01:30 PM #2
Then buttonsList is null. Use Code tags when posting code.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-16-2010, 01:31 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
It's valid to add a null entry to a List, so it really doesn't matter if button1 is null or not. buttonsList is what's null, not button1. My guess is that you are either setting it to null in the ommited code, or redefining a local buttonsList and failing to initialize it.
- 03-17-2010, 03:14 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please send the omitted code segment here to look at.
And also please use code tags next time when you post codes here in the forum.
- 03-22-2010, 05:06 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
thanks for help, problem has been sorted
- 03-22-2010, 05:19 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Then please mark the thread solved, from tool menu.
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 03:22 AM -
Null pointer exception
By talha06 in forum JDBCReplies: 5Last Post: 07-14-2009, 01:12 AM -
Help with null pointer exception
By gammaman in forum New To JavaReplies: 4Last Post: 07-14-2009, 12:23 AM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks