Search error
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)
Related Article: Multiple text file search
is a solved Java discussion thread by FUTURECompEng that has 11 replies, was last updated 1 year ago and has been tagged with the keywords: arraylist, java, files, search, loops, class.
Ashenvale
Junior Poster in Training
72 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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.
stultuske
Industrious Poster
4,374 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
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.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16