Get number of not-null fields in Java String array -
i created string array 100 fields.
then, read in values file.
there 7 values, number vary anywhere 1 100 left string array 100 fields.
essentially, have key value use identify other values equal.
however, when try check array values equal key value, receive null pointer exception:
for(i = 0; <= membertype.length; i++) { if(membertype[i].equalsignorecase(membertypeinput)) { out.println(membercode[i] + " " + membertype[i] + " " + firstname[i] + " " + lastname[i] + " " + age[i] + " " + gender[i]); }// end if }// end from reading several other similar (but not same) questions/answers, , googling problem few hours, understand null pointer exception occurs because trying check key value against values null.
my question is: best way avoid null pointer exception in case?
more specifically: how java find number of not-null fields? think if can find number of not-null fields, can java check key value against not-null fields, avoiding null pointer exception.
i tried finding number of not-null fields with:
int index = -1; (i = 0; < membertype.length; i++) { if (!membertype[i].equals(null)) { index = i; } } and used "index" in loop search array values equal key value.
however, above code throws me null pointer exception.
in case want check out, of code below. there other stuff going on, wanted leave in whole picture. null pointer exception points line 157. it's if statement.
package phase2; import java.io.*; import java.util.*; import javax.swing.joptionpane; import static java.lang.system.out; public class phase2 { // defining arrays & variables // ///////////////// // main method // ///////////////// public static void main (string[] args) { int menuselection; start_program(); menuselection = menu(); switch(menuselection) { case 1: modify_member(); break; case 2: modify_registration(); break; case 3: report(); break; case 4: end_program(); system.exit(0); }// end switch case statement }// end main method //////////////////////////////////////////// // method run once @ start of program // //////////////////////////////////////////// public static void start_program() { //vars & arrays start_program() method string newline; int count = -1; int[] membercode = new int[100]; string[] membertype = new string[100]; string[] firstname = new string[100]; string[] lastname = new string[100]; int[] age = new int[100]; char[] gender = new char[100]; int menuselection = 0; string menuoutput; int i; string membertypeinput; try { bufferedreader membersfile = new bufferedreader(new filereader("members.dat")); while ((newline = membersfile.readline()) != null) { stringtokenizer delimiter = new stringtokenizer(newline, "#"); count = count+1; membercode[count] = integer.parseint(delimiter.nexttoken()); membertype[count] = delimiter.nexttoken(); firstname[count] = delimiter.nexttoken(); lastname[count] = delimiter.nexttoken(); age[count] = integer.parseint(delimiter.nexttoken()); gender[count] = delimiter.nexttoken().charat(0); }// end while membersfile.close(); }// end try catch(ioexception error) { out.println("there error on file read " + error); }// end catch ////////// // menu // ////////// menuoutput = "\tmoon area ymca" + "\n" + "1. member info" + "\n" + "2. members of specific type" + "\n" + "3. members between specific age range" + "\n" + "4. members of specific gender" + "\n" + "5. report 5" + "\n" + "6. report 6" + "\n" + "7. report 7" + "\n" + "8. report 8" + "\n" + "9. report 9" + "\n" + "10. exit report menu" + "\n\n" + "please make selection"; while(menuselection != 10) { menuselection = integer.parseint (joptionpane.showinputdialog(null, menuoutput, "main menu", joptionpane.question_message)); switch(menuselection) { case 1: (i=0; i<=count; ++i) { out.println(membercode[i] + " " + membertype[i] + " " + firstname[i] + " " + lastname[i] + " " + age[i] + " " + gender[i]); }// end break; case 2: membertypeinput = joptionpane.showinputdialog (null, "please enter member type", "info member type", joptionpane.question_message); if (membertypeinput.equalsignorecase("single") || membertypeinput.equalsignorecase("family") || membertypeinput.equalsignorecase("couple")) { // getting number of non-null fields in membertype array //// need find number of not null fields /*int index = -1; (i = 0; < membertype.length; i++) { if (!membertype[i].equals(null)) { index = i; break; } }*/ // getting index number of each membertypeinput value in array for(i = 0; <= membertype.length; i++) { if(membertype[i].equalsignorecase(membertypeinput)) { out.println(membercode[i] + " " + membertype[i] + " " + firstname[i] + " " + lastname[i] + " " + age[i] + " " + gender[i]); }// end if }// end }// end if else { joptionpane.showmessagedialog (null, "please choose 'single', 'couple', or 'family'", "error", joptionpane.information_message); }// end else break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; case 8: break; case 9: break; case 10: break; }// end switch }// end while }// end start_program method ////////////////////////////////////////////////////////// // method print menu , return user input selection // ////////////////////////////////////////////////////////// public static int menu() { string menuselectionstring; int menuselection = 0; string output; while(menuselection == 0) { output = "moon area ymca\n\n" + "1. add/modify member information\n" + "2. add/modify class registrations\n" + "3. report section\n" + "4. exit system\n\n" + "please make selection:" ; menuselectionstring = joptionpane.showinputdialog(null, output, "main menu", joptionpane.question_message); if (menuselectionstring.equals("1") || menuselectionstring.equals("2") || menuselectionstring.equals("3") || menuselectionstring.equals("4")) { menuselection = integer.parseint(menuselectionstring); }// end if statement else { joptionpane.showmessagedialog(null, "please select number between 1 , 4.", "error", joptionpane.information_message); menuselection = 0; }// end else statement }// end while loop return menuselection; }// end menu method //////////////////////////////////////////////////////////// // method add, delete, change basic member information // //////////////////////////////////////////////////////////// public static void modify_member() { out.println("modify_member() method has executed."); }// end modify_member method ////////////////////////////////////////////////////////////////// // method add, delete, change class registration information // ////////////////////////////////////////////////////////////////// public static void modify_registration() { out.println("modify_registration() method has executed."); }// end modify_registration method ///////////////////////////// // method print reports // ///////////////////////////// public static void report() { out.println("report() method has executed."); }// end report method ////////////////////////////////////////// // method run once @ end of program // ////////////////////////////////////////// public static void end_program() { out.println("end_program() method has executed."); }// end end_program method }// end phase2 class any appreciated!
you can't call methods on null; if methodtype[i] contains null reference, trying call method on results in nullpointerexception. check it's null first, using !=:
if (membertype[i] != null && membertype[i].equals("whatever")) { ... this short-circuit in event membertype null, rest of expression (after &&) won't evaluated.
Comments
Post a Comment