Reputation: 13
I wrote some code that sets the Image on the JLabel. The path of Image is obtained from getSelectedValue() method for selected item on the JList.
The Jlist was populated when user clicked search button by typing the item to be searched in JTextField(i.e searchTextField) after choosing any option out of three items in JComboBox(i.e typeChooserBox). Then my program reads saved records from a file "Records.txt" and breaks into tokens to compare proper token(based on index of typeChooserBox) with the user input value(in searchTextField). Then it populates JList.
Below is the event handler for search Button that populates JList.
public class searchButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
model.clear();//This empties the JList creating nullpoint Ex
int index=typeChooserBox.getSelectedIndex();
String toCompare=searchTextField.getText();
try {
File file = new File("Records.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line=null;
while((line=reader.readLine())!=null){
String[] tokens = line.split("/");
if( index==0){
if(tokens[0].equals(toCompare))
model.addElement(tokens[2]);}
if(index==1){
if(tokens[1].equalsIgnoreCase(toCompare))
model.addElement(tokens[2]);}
if(index==2){
if(tokens[3].contains(toCompare))
model.addElement(tokens[2]);}
}
}
catch(FileNotFoundException e2){
JOptionPane.showMessageDialog(null,"File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
catch(IOException ex){
JOptionPane.showMessageDialog(null,"File not found", "Error",JOptionPane.ERROR_MESSAGE);
}
}
}
When I search for a record. For the First time -my JList gets populated with results of search .Then when I select an item from JList, it works(sets the correct image on JLabel) until I perform a new search. As soon as I click the search button(second time). My program throws nullPointExecption and stops working. Sorry I have not included SSCCE. But if these little information is not sufficient please let me know. :)
WELL I FIGURE OUT MY PROBLEM... the code model.clear() clears all the item in the list when a search button is clicked. Due to this reason, there will be no selected item at the list resulting null pointer exception. But How do I solve this. Can I clear all the field but avoid null value when getSelctedValue() is called for the JList.
I tried doing this but it still didn't work.
public class searchListListener implements ListSelectionListener {
String s;
String imagePath;
public void valueChanged(ListSelectionEvent evt){ try{
imagePath= (String) searchResult.getSelectedValue();
ImageIcon image = new ImageIcon(imagePath);
imageLabel.setIcon(image);
searchResult.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
catch(NullPointerException ne){
JOptionPane.showMessageDialog(null, "NullPointerException");
model.addElement(s);
searchResult.setSelectedValue(s, true);
}
finally{
s=imagePath;
}
}
}
public class typeChooserBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent ev){
}
}
I am new on this java world, Sorry if I have not been able to help you to help me.:(
Upvotes: 1
Views: 1749
Reputation: 1229
It is hard to tell without the stack trace of your exception or what happens in searchResult.getSelectedValue().
Perhaps try this:
public void valueChanged(ListSelectionEvent evt){
if( evt.getValueIsAdjusting() ) return;
// your code here
}
Upvotes: 1