Hi, I'm trying to insert a search function into my program. I used 2 classes

class PatientInfo{
    private void BAddActionPerformed(java.awt.event.ActionEvent evt) {                                     


     PatientAddress pInfo = new PatientAddress(); 
                 lName = txtLastName.getText();
            pInfo.getLName(lName); 
            txtLastName.setEditable(false);
    }       

     private void BSearchActionPerformed(java.awt.event.ActionEvent evt) {
            PatientAddress pInfo = new PatientAddress(); 

            lName = txtSearchName.getText();
            pInfo.searchInfo(lName);
            txtLastName.setText(pInfo.lastName);

    }
}   






public class PatientAddress extends PatientInfo{
    public int ctr, loc;


    public String lastName;
    public String[] lNameArr = new String[50];

    public void getLName(String lName){ 
      for(ctr=0; ctr<lNameArr.length;ctr++)
        {
            lNameArr[ctr] = lName;

        }

    }


public void searchInfo(String lName){

         loc = -1;
         for(ctr=0;ctr<lNameArr.length;ctr++)
             if(lNameArr[ctr].equalsIgnoreCase(lName))
            {
             loc=ctr;
             break;
            }
          if(loc>=0){
              lNameArr[ctr] = lastName;

          }


    }   

the thing is whenever I click the search button, I get this error. can anyone tell me what's this error means?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hospitalbilling.PatientAddress.searchInfo(PatientAddress.java:32)

your code makes no sense at all.

I'll try to get you up to speed:

this part

PatientAddress pInfo = new PatientAddress(); 
            lName = txtSearchName.getText();
            pInfo.searchInfo(lName);

will first instantiate a new PatientAddress, which has it's own array of Strings of 50 elements (you aren't adding your address to it, and the next instance won't have any information about previous addresses)

public String[] lNameArr = new String[50];

well, that creates your array. an array of 50 default values for null, being null, since you don't initialize them.
two lines later, you cann the searchInfo method, which contains this lines:

for(ctr=0;ctr<lNameArr.length;ctr++)
             if(lNameArr[ctr].equalsIgnoreCase(lName))

so, it will iterate over all the fifty elements (even though every time you run this method, the array will be completely empty - fifty null's) and call the equalsIgnoreCase method on that item. if you call a method on a null, you get a nullpointerexception.

now, if you want your array to keep track of the data already entered, you have to:
(either) make it class level, static variable,
or keep the array in the calling class that calls the uses the PatientAddress class.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hospitalbilling.PatientAddress.searchInfo(PatientAddress.java:32)

There is a variable on line 32 with a null value.

This article has been dead for over six months. Start a new discussion instead.