Android Forums

Go Back   These FORUMS ARE DISABLED!! OUR NEW FORUMS ARE AT http://androidforum.com/ > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 07-31-2008, 03:06 PM   #1
Junior Member
 
Join Date: Jul 2008
Posts: 5
Friends: 0
View baran_khan's Profile   View baran_khan's Photo Album   View baran_khan's Blog   View Social Groups
Hi,

I am facing this issue with very simple code. All I have done is declare a button in the xml layout and calling it in the onCreate() method. What I am receiving is a Null Pointer exception. I have wasted two days hopping around..still no idea whats wrong with that...

Hopefully someone can sort this thing out...The code is as below:
PHP Code:
public void onCreate(Bundle icicle) {
        
super.onCreate(icicle);
        
setContentView(R.layout.main);
        
temp = (Button)findViewById(R.id.button);
        
temp.setOnClickListener(new View.OnClickListener(){

            public 
void onClick(View arg0) {
                
// TODO Auto-generated method stub
                
            
}
            
        });
    } 
Hope to hear something great
baran_khan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-31-2008, 04:35 PM   #2
Member
 
Rand5's Avatar
 
Join Date: Apr 2008
Posts: 35
Friends: 0
View Rand5's Profile   View Rand5's Photo Album   View Rand5's Blog   View Social Groups
Here's a nice little trick to find the line that is causing the NPE

1. Open the Debug perspective in Eclipse
2. Choose the 'Breakpoints' view tab.
3. Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
4. Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
5. Execute the offending workflow. It will break on the line that
caused the NPE.
Rand5 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-31-2008, 11:07 PM   #3
Junior Member
 
Join Date: Jul 2008
Posts: 5
Friends: 0
View baran_khan's Profile   View baran_khan's Photo Album   View baran_khan's Blog   View Social Groups
Quote:
Originally Posted by Rand5 View Post
Here's a nice little trick to find the line that is causing the NPE

1. Open the Debug perspective in Eclipse
2. Choose the 'Breakpoints' view tab.
3. Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
4. Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
5. Execute the offending workflow. It will break on the line that
caused the NPE.
Hi,

Thanks for the reply, well I have already done that and I know where the exception is but the problem is I can't resolve it. The problem is on the line where I am fetching the button from the xml. For some reasons it is returning null instead of the button I am referring to.

I verified the name, the button is present in the xml and is been identified by eclipse as well. That means I can read that in the class as it is listed in the R.java. Now every time I run the application I get this null pointer exception. I tried different systems and I tried multiple applications, I guess its only me who is making the mistake and missing something in the process development. Can anyone provide me some help or else sample code that have only single button on the screen which is doing a very small thing...lets say changing the text of a editbox.

Thanks in advance...
baran_khan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-31-2008, 11:47 PM   #4
Junior Member
 
Join Date: Jul 2008
Posts: 5
Friends: 0
View baran_khan's Profile   View baran_khan's Photo Album   View baran_khan's Blog   View Social Groups
Got it....The problem was in the syntax.....I found this thing on a forum that the google has changed the convention of declaring an ID for a field in the xml layout from "id=name" to "android:id=name" ...A thing which they should have mentioned on there website.....

Wasted my two full important days in identifying this foolish thing....

anyways..hope this will help someone else by reading this post....
baran_khan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 08-30-2011, 09:21 AM   #5
New Member
 
Join Date: Aug 2011
Posts: 1
Friends: 0
View bugsworld85's Profile   View bugsworld85's Photo Album   View bugsworld85's Blog   View Social Groups
there is also a quicker way to debug that error.. go to the logCat tab just beside the console tab. you will see what caused the NPE error. you will see there what method that created that error. and it will also show you the exact line to where the error occured. you can see on what line by just looking at the last numbers designated error.
bugsworld85 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 11-25-2011, 06:32 AM   #6
New Member
 
Join Date: Nov 2011
Posts: 1
Friends: 0
View neoplanet's Profile   View neoplanet's Photo Album   View neoplanet's Blog   View Social Groups
I get this annoying error often on Eclipse Helios and ADV.
I don"t know why, but suddenly got NPE on a part of program what not changed and it was worked correctly before..
The solution is rebuild the xml layout window, sometimes just create an other and copy the xml source and change the contentView.
Debugging is not helping in this case.
neoplanet is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 11-25-2011, 10:11 AM   #7
Ronin1770
 
Join Date: Oct 2011
Posts: 1
Friends: 0
View ronin1770's Profile   View ronin1770's Photo Album   View ronin1770's Blog   View Social Groups
Clean the Project and rebuild

Been there and done that
ronin1770 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes




Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 05:46 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.