Java. lang. NullPointerException, pointerexception when listView. getChildAt (I)

Source: Internet
Author: User
Tags call back

Java. lang. NullPointerException, pointerexception when listView. getChildAt (I)

BaseAdapter returns the component that can display the number of items on the current screen. Therefore, it uses listView. getChildAt (I );

The returned result is a widget that can be displayed on the current screen. You cannot use the listView. getChildAt (I) method to retrieve widgets other than those that can be displayed on the current screen. The following method is incorrect:

For (int I = 0; I <listView. getCount (); I ++) {CheckBox checkBox = (CheckBox) listView. getChildAt (I); // This method is used to return the components in all the items of the ListView, but it is incorrect .}

The following exception occurs:


View the source code:


In this case, the object obtained through listView. getChildAt (I); should be null, And the null object has no attributes and Methods. Therefore, a null pointer exception occurs.

Correct practice:

List. setOnItemClickListener (new OnItemClickListener () {@ Override public voidonItemClick (AdapterView <?> Parent, View view, int position, longid) {// TODO Auto-generated method stub CheckBoxcheckBox = (CheckBox) view. findViewById (R. id. cbSelect); checkBox. toggle (); if (checkBox. isChecked () {// Add the phone number sendList for this list item. add (numList. get (position); sendName. add (nameList. get (position);} else {// remove the phone number and name sendList from the group list. remove (numList. get (position); sendName. remove (nameList. get (position ));}}});


To obtain the selected Item, this method selects to call back OnItemClickListener when the user clicks an Item to obtain the component (that is, the adapter) in the clicked Item.PublicView getView (IntPosition, View convertView, ViewGroupparent), and find its child component. Then perform operations on the child components.


JavalangNullPointerException in Java running

Because the directory may be empty, that is, there are no files in the directory, So path. list () returns null, so you need to judge before the for loop and add an if (files! = Null. The complete procedure is as follows:
Import java. io. File;
Import java. io. IOException;
Public class traverse {
Public static void tra (File path ){
If (path = null ){
Return;
}
If (path. isDirectory ()){
String [] files = path. list ();
If (files! = Null) // Add a sentence here
For (int I = 0; I <files. length; I ++ ){
Tra (new File (path, files [I]);
}
} Else if (path. getAbsolutePath (). endsWith (". mp3 ")){

System. out. println (path );
}
}
Public static void main (String [] args ){
Try {
File file = new File ("D :\\");
Tra (file );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

What should I do if javalangnullpointerexception occurs?

At calendar. PrintMonthBody (calendar. java: 515)
At calendar. PrintMonth (calendar. java: 507)
The error is not required in these two lines. as for NullPointerException, to put it bluntly, it means that the value you want is not obtained. If null occurs, check whether your program is running. (if not, check whether this line is correct)
Int startday = GetStartDay (year, month );

// Get number of days in the month
Int dayinmonth = GetNumOfDaysInMonth (year, month );
The NullPointerException handling method is very easy to handle. It's okay to look up the problem at the first level to see where the value is not obtained. There are many ways to query it. You can use the debug mode, or print the statement in the program to judge.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.