Short code causes a strange problem - About the list again -- please read!

Hi again people. Maybe you remember my project - has a list, that you can search thru using a text field. During the work I got stuck on a strange problem ( Again :-( ) My app has one text field, one combo box, one list and a text field once more. The code should do the following ->
*1. Load the list, no problem with that.*
*2. Show the elements of the list, that match the selected group in the combo box,no problem.*
*3. Search thru the list using the text field,no problem.*
4. When the user selects an element from the list, it should display its info in the second text field. This also works fine, but when after looking at info of one of the elements the things on numbers 2 and 3 ( look up! ) stop working. I must say that everything works fine until user selects an element from the list. I couldnt understand this kind of behavior so I am asking you to help me please.
The code is very simple:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class the_window extends JFrame implements DocumentListener, ItemListener, ListSelectionListener {
    FileReader reader;
    String data_base[][];
    String first_pass[];
    int number_of_elements;
    DefaultListModel dflm = new DefaultListModel();
    JList list;
    JTextField text_field = new JTextField();
    JTextField info_field = new JTextField();
    String groups[] = {"1. group" , "2. group"};
    JComboBox groups_cmbx = new JComboBox(groups);
    the_window(){
        super("the Window!");
        JPanel panel = new JPanel(null);
        Container c = this.getContentPane();
        c.add(panel);
        text_field.setBounds(10,10,170,25);
        text_field.getDocument().addDocumentListener(this);
        panel.add(text_field);
        groups_cmbx.setBounds(10,45,170,25);
        groups_cmbx.addItemListener(this);
        panel.add(groups_cmbx);
        list = new JList(dflm);
        list.setBounds(10,90,170,190);
        list.setFixedCellHeight(20);
        list.addListSelectionListener(this);
        panel.add(list);
        info_field.setBounds(10,280,170,25);
        panel.add(info_field);
        load_the_base();
        refresh();
        this.setSize(190,350);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
    public void itemStateChanged(ItemEvent e){
        refresh();
    public void valueChanged(ListSelectionEvent e){
        String str = (String) dflm.getElementAt(list.getSelectedIndex());
        int index = 0;
        for(int i = 0; i < number_of_elements; i++){
            if(str.equals(data_base[0])){
index = i;
break;
info_field.setText(data_base[index][1]);
private void load_the_base(){
String data = "";
try{
reader = new FileReader("data.txt";);
int r = 0;
while((r = reader.read()) != -1){
char c = (char) r;
data += c;
reader.close();
}catch(IOException e){}
first_pass = data.split(";");
number_of_elements = first_pass.length;
data_base = new String[number_of_elements][];
for(int i = 0; i<number_of_elements; i++){
data_base[i] = first_pass[i].split("#");
private void refresh(){
String search_str = text_field.getText();
int selektovano = groups_cmbx.getSelectedIndex();
dflm.clear();
for(int i = 0; i < number_of_elements; i++){
int grupa = Integer.parseInt(data_base[i][2]);
if(grupa == selektovano){
String at_the_moment = data_base[i][0]; // if you change this to String at_the_moment = data_base[i][1]; it works perfectly
if(at_the_moment.startsWith(search_str)){
dflm.addElement(at_the_moment);
public void changedUpdate(DocumentEvent e){
refresh();
public void removeUpdate(DocumentEvent e){
refresh();
public void insertUpdate(DocumentEvent e){
refresh();
public class Main {
public static void main(String[] args) {
JFrame f = new the_window();
Now, can you please tell me whats wrong with this?
For the "data.txt" make a new text file using *notepad* and copy the following line into the document:
_1. element#1. info#0;2. element#2. info#0;3. element#3. info#1;4. element#4. info#1;5. element#5. info#1;_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Darryl.Burke wrote:
Keith, thanks for making that readable. So here's the diagnosis -
In the refresh() method, calling defaultListModel.clear() results in a valueChanged(...) event in which this method calldefaultListModel.getElementAt(list.getSelectedIndex())results in the exception noted, as getSelectedIndex returns -1, the list being empty... you can't getElementAt(-1).
I haven't analyzed all the code nor checked whether is now works as desired, but this small change to valueChanged counters the exception being thrown.   public void valueChanged(ListSelectionEvent e) {
infoField.setText(""); // do this unconditionally
if (list.getSelectedIndex() != -1) {
String value = (String)defaultListModel.getElementAt(list.getSelectedIndex());
for(int i = 0; i < numFields; i++){
if(value.equals(matrix[0])){
infoField.setText(matrix[i][1]);
break;
db
Yea! You were right! I didnt think that calling *list_model.clear();* will result in calling *valueChanged()* ........
That was some *clear()* thinking :-) Thank you!
corlettk wrote:
I cleaned up some variable & method names (tut tut), imports (very naighty), and some thread stuff... but it remains fundamentally the same codeIs it so important to "clean" the imports? How much does it slow down the loading time? Should I do this on all my projects, because they are all "very naighty"?
ps. Thanks to all that gave some help to answering this strange question :-)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Strange problem with the mail app on an iPad

    I've got a bit of a strange problem with the mail app on an iPad - it's sending and receiving mail ok but won't let you delete mail. If you keep at it, it appears as if you have deleted mail (though takes a very long time as you just have to keep deleting it over and over again) -  but the size of the app still keeps going up - reached 600mb so far, even though there is hardly any mail or attachments (goes up quite a lot each time you try to delete anything) - any idea what's causing this and how I can fix it?
    It occasionally comes up with a dialogue panel saying to check my account settings, but I have checked those and they are fine. The little 'loading' icon just keeps spinning constantly too.

    go to Settings > Mail > your account > Advanced > Move Discarded Messages Into 
    and make sure that Deleted Mailbox is ticked, and not Archive Mailbox
    You can also adjust how long the deleted mail stays in the trash immediately below that.
    If you are talking about a gmail account, you may also have to login to your account on the gmail server and update the settings there.  This is what Barney previously posted about that:

  • The problem about the signal of JNI

    Hello,
    When the system (it creates by the C language) created here is operated on Solaris and javaVM is operated using a JNI, the phenomenon which the core file outputs within the JIT compiler of java (libsunwjit.so) has occurred.
    Although it had generated twice until now, since it had collided with the signal which the signal has generated in one of the another threads, and it uses internally by JavaVM from the contents of a core file when it generated first, it had generated.
    However, although the core file was investigated when it generated at the 2nd times, there was no trace that another thread generated the signal.
    Is there any case where collide with the signal currently used by JavaVM, and also a signal is generated in a JIT compiler?
    What thing has an obstacle used as the factor which a JIT compiler makes have generated the signal in the past again?
    Please give me the reply to the above-mentioned question for a cause elucidation.
    As appending data, the contents which referred to the core file are described below.
    (gdb) thread 1
    [Switching to thread 1 (LWP    163        )]
    #0 0xfe359d88 in __sigprocmask () from /usr/lib/libthread.so.1
    #1 0xfe34eb34 in __sigredirect () from /usr/lib/libthread.so.1
    #2 0xfe351a10 in thrpkill_unlocked () from /usr/lib/libthread.so.1
    #3 0xfe239470 in abort () from /usr/lib/libc.so.1
    #4 0xfb49353c in panicHandler ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/classic/libjvm.so
    #5 0xfb798084 in intrDispatchMD ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/native_threads/libhpi.so
    #6 0xfe359348 in __libthread_segvhdlr () from /usr/lib/libthread.so.1
    #7 0xfe35bdf0 in __sighndlr () from /usr/lib/libthread.so.1
    #8 0xfe3586f8 in ?? () from /usr/lib/libthread.so.1
    #9 0xfb3bd8bc in JITPerformDynamicPatch ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/libsunwjit.so
    #10 0xfb3de848 in JITResolveConstPoolEntry ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/libsunwjit.so
    #11 0xb945908 in ?? ()
    #12 0xc5054e4 in ?? ()
    #13 0xe2974c in ?? ()
    #14 0x851b75c in ?? ()
    #15 0x7c3841c in ?? ()
    #16 0xb917560 in ?? ()
    #17 0x8a9b6d4 in ?? ()
    #18 0xb8e18e0 in ?? ()
    #19 0x71377d8 in ?? ()
    #20 0x7137784 in ?? ()
    #21 0x7d7d7f4 in ?? ()
    #22 0xc67c4b0 in ?? ()
    #23 0x913998c in ?? ()
    #24 0xb2bf58 in ?? ()
    #25 0xc51a854 in ?? ()
    #26 0x8794c7c in ?? ()
    #27 0xfb3de980 in JIT_INVOKER_MARKER ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/libsunwjit.so
    #28 0xfb494cec in notJavaInvocation ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/classic/libjvm.so
    #29 0xfb457674 in jni_Invoke ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/classic/libjvm.so
    #30 0xfb45930c in jni_CallVoidMethod ()
    from /opt/fujitsu/jasmine/JasEMedia/jre/lib/sparc/classic/libjvm.so
    #31 0xfbee0d7c in ChartJem_Generate ()
    from /opt/fujitsu/jasmine/EMImageChart/lib/GKM0JEMC.so
    #32 0xfb7630a4 in ?? ()
    from /opt/fujitsu/jasmine/data/default/methods/JasEMedia/lib043C.so.2.0
    #33 0xff2b106c in odb_OmsPcExec () from /opt/fujitsu/jasmine/lib/libjas.so
    Please contact me, if the core file is required. Separately, I will send.
    In that case, also about the transmission method, please unite and contact me.
    The system configuration is as follows.
    Operation System:SOLARIS(7)R00081
    Java version:1.2.2 Classic VM (build JDK-1.2.2-W, green threads, sunwjit)
    I'm hoping someone can help me out.
    Thanks!

    After that, a problem is not solved. Would you teach, if there is the method of something saying?

  • How to solve illustrator cs6 save as cs5 problem about the stroke(when the stroke with clipping mask and color is gradient, save as cs5 will change to embed ).

    how to solve illustrator cs6 save as cs5 problem about the stroke(when the stroke with clipping mask and color is gradient, save as cs5 will change to embed ).

    Because it was not possible to apply a gradient to a stroke in CS5. When you open the file in CS5, it is reduced to something that can be rendered in CS5.

  • Strange problem with the internet connection

    Hello,
    I've a strange problem with the network connection , I have bought a Iphone 3g in Italy in date 31st
    October 2010. Now in Italy I have just subscribed a telephone rental with Wind company, where there is an unlimited internet service. The problem is that in my Iphone there is not the line.
    All this is strange because I receive the messages from social networks (like facebook and msn) but I cannot open tha page for the internet connection. I have gone to Apple store and I have tried to put my SIM in an other phone (iphone 3g or 3gs): my SIM is perfectly working.
    The problem is only for the internet connection on my phone because wifi connection is good.
    Thanks!!

    Please help everyone who has the same problem by posting yr solution, thx!

  • Strange problem when validating XML agains DTD file.

    Dear experts,
    I'm write a simple code snippet that validates my XML file agains a given DTD.
    It allways report the following error:
    org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.
         at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
         at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)Althought, my xml file is OK with Xmlspy.
    My code is:
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                             "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          factory.setValidating(true);
          factory.setAttribute(
              "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
              "http://www.w3.org/2001/XMLSchema");
          factory.setAttribute(
              "http://java.sun.com/xml/jaxp/properties/schemaSource", SchemaUrl);
          DocumentBuilder builder = factory.newDocumentBuilder();
          Validator handler = new Validator();
          builder.setErrorHandler(handler);
          builder.parse(new java.io.FileInputStream(new java.io.File(XmlDocumentUrl)));And here are the xml file:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE capsule SYSTEM "c:/SampleFiles/capsule.dtd">
    <capsule>
         <header>
              <name>Capsule 1</name>
              <author>Author</author>
              <company>Company</company>
              <last-save-time>Fri Apr 01 14:59:21 GMT+07:00 2005</last-save-time>
              <description>description</description>
         </header>
         <datasets>
              <dataset type="input">
                   <name>dataset1</name>
                   <description></description>
                   <columns>
                        <column type="fromsource">
                             <name>partno</name>
                             <data-type>varchar</data-type>
                             <length>80</length>
                             <precision/>
                             <scale/>
                             <allow-null>false</allow-null>
                             <default-value/>
                        </column>
                        <column type="notfromsource">
                             <name>balanceonhand</name>
                             <data-type>integer</data-type>
                             <length/>
                             <precision/>
                             <scale/>
                             <allow-null>false</allow-null>
                             <default-value/>
                        </column>
                   </columns>
                   <rule>LET a=update; LET b=employye set salary = 100; print a + b</rule>
                   <!--output: update employee set salary = 100-->
                   <command>inmemory (getvalue_dataset("dataset1".rule))</command>
                   <params>
                        <param>
                             <name/>
                             <type/>
                        </param>
                   </params>
              </dataset>
         </datasets>
    </capsule>What I can do now? Change my xml file?
    Please provide some hints.
    Thanks in advance.

    I tried to remove the DOCTYPE, but the problem stayed the same.
    Thanks.

  • When I try to manually backup my ipad2, I receive an error message stating "There was a problem completing the backup.  Please try again later" ... and this happens each time I try to back up.  Grateful for suggestions ...

    When I try to manually backup my ipad2, I receive an error message stating "There was a problem completing the backup.  Please try again later" ... and this happens each time I try to back up.  Grateful for suggestions ...

    Seems to be a common enough problem. I've deleted the partial failed backup from iCloud, restoring all 5Gb of storage. Then started a manual backup. After about 20 minutes I get the same error message. I have repeated this several times. According to my iTunes on my laptop I managed one backup to iCloud from there but it is pointless if u can neither backup to or restore from iCloud via my iPhone 4. I had hoped ios 5.0.1 would solve this glitch but ....
    So much for "it just works".
    I have yet to see any suggested fix that works for me. Any other suggestions?

  • I would like to create a playlist of some of my voicememos to hear on my iphone5. I have no problem creating the list and can listen on playlist on computer but not when i sync to my phone. How can I listen to voicememos in a playlist on my iPhone?

    I would like to create a playlist of some of my voicememos to hear on my iphone5. I have no problem creating the list and can listen on playlist on computer but not when i sync to my phone. How can I listen to voicememos in a playlist on my iPhone?

    Hi czigrand,
    Thanks for visiting Apple Support Communities.
    Currently, you can gift dollar amounts or individual items (EG. songs) on the iTunes Store. See this article for more information:
    In the iTunes Store, you can gift a dollar amount or specific music, movies, TV shows, or apps from your iPhone, iPod touch, iPad, Mac, or PC. Follow these steps to send a gift from the iTunes Store.
    iTunes: Sending iTunes Gifts
    http://support.apple.com/kb/HT2736
    Best Regards,
    Jeremy

  • How to remove 1797 emails. stuck in inbox.  but not showing anymore - and i have the same problem wit the sent.  please help

    how to remove 1797 emails. stuck in inbox.  but not showing anymore - and i have the same problem wit the sent.  please help

    This is a user supported frum, so making threats really doesn't help, besides which it's not like any of us can really make a dent in Toshiba's bottom line despite how many people we think we can influence. Unless those people were standing in line to buy a Toshiba product cash in hand, and you pulled them out, it really doesn't add up for them.
    At this point it would probably be better for you to use the 800 number. Have all your e-mails ready to forward, if needed, to whoever you end up talking to. Don;t let them off the hook. An hour on the phone is much better than weeks passing e-mails through a support site. Also contest the charge with your bank or credit card.

  • I have a problem with the mac can not read video files with the extension IMOD. How can I solve this problem?

    I have a problem with the mac can not read video files with the extension IMOD. How can I solve this problem?

    By doing a Google search. 

  • I am English but I live in Spain. when I try to download an app now, as well as my password i get asked to complete a security question from a list. The problem is the list is in Spanish. How can I turn this additional security off

    I am English but I live in Spain. when I try to download an app now, as well as my password i get asked to complete a security question from a list. The problem is the list is in Spanish. How can I turn this additional security off.
    Please note: I dont speak Spanish.
    Please HELP

    You can't turn it off.

  • TS3376 I was able to use the findmyiphone app last week, but now I have been getting an error message "Can't load findmyiphone.  There was a problem loading the application."  please help as we are trying to locate an ipod with pics of a no deceased loved

    I was able to use the findmyiphone app last week, but now I have been getting an error message "Can't load findmyiphone.  There was a problem loading the application."  please help as we are trying to locate an ipod with pics of a no deceased loved one.

    Had the same problem and the same message --  was suddenly unable to access icloud apps on my pc, also using Windows 7 and Internet Explorer. After several days of pulling my hair out, decided to try a different browser and was able to get in by using Firefox instead of Internet Explorer.  Hope this helps. 

  • D wont backup to icloud. it is enabled but i get  a message "There was a problem completing the backup. Please try again later. I have tried multiple times over the last 24 hrs. My iphone backs up to icloud fine

    Mo ipad 2 won't backup to icloud. I get a message "There was a problem completing the backup. Please try again later. I have tried multiple times over the last 24hrs but no luck. My iphone backs up fine on the same wireless network.

    i have this issue to any solution yet?

  • About the new Pages, please.

    About the new Pages, please. I've a lot of text in the old '09 version 4.3 -- how do I go about moving it to the new Pages?

    Many thanks for your message, Peter. So glad to have your reply and impressed with all you’ve contributed to this topic here in the past.
    Guess I was hoping that by looking away long enough, things would get better. I’d put aside my book, all in Pages ’09, but now I want to finish it and to move what’s done to Scrivener. Surely there’s some better way to rescue the old stuff than a complete rewrite?
    Seems Scrivener actually uses TextEdit and that’s fine but of course I was using Pages in the first place because I’d like more control of how stuff looks on the page from the start.
    Please tell me: Without losing any text, what are the options?
    Kind regards . . . Fortes

  • How to determine the source to where my saved photo (not taken by my iPhone camera) on whether what application or website did i got that photo? need to go to the source again, please help

    how to determine the source to where my saved photo (not taken by my iPhone camera) on whether what application or website did i got that photo? need to go to the source again, please help

    There is no way to trace the source of a saved photo.
    You might have some luck if you search through the Safari web site history available via the Bookmark icon.

Maybe you are looking for