Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default code won't put filenames into combobox?

    Hello everyone once more,


    I have code for going to a directory, then when inside the specified folder the code is supposed to take the filenames of every file in the folder and list it as a select-able item. I took the code from another application which I made that works, so I know the code should work as well.


    For reasons I can't explain the output indicates that the loop "setup ();" is seriously loathed by the IDE.

    Java Code:
    public class ComponentFilli extends javax.swing.JFrame {
    
        /**
         * Creates new form ComponentFilli
         * @throws java.io.FileNotFoundException
         */
        public ComponentFilli() throws FileNotFoundException, IOException {
            initComponents();
            setResizable(false);
            setup();
            // jComboBox1.setModel(new DefaultComboBoxModel(files));
             fileReader = new CsvFileReader(templateFile);
            jComboBox3.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(TANTALUM)));
            jComboBox4.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(ANODE)));
            jComboBox5.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(ANODE)));
            jComboBox6.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(ANODE)));
            jComboBox7.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(ANODE)));
            jComboBox8.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(ANODE)));
            jComboBox9.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(CATHODE)));
            jComboBox1.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(GLASSING)));
            jComboBox2.setModel(new DefaultComboBoxModel(fileReader.getBoxItems(GLASSING)));
        }
    private void setup() throws IOException{
       
        fileList=folder.listFiles();
         jComboBox1.setModel(new DefaultComboBoxModel(files));//tells combobox to put the file names into the menu
            files = new String[fileList.length];  // files equals the length of filelist
            
            for(int i = 0; i < fileList.length; i++){ //for i=0; i< the length of file list; add 1 to i
                files[i] = fileList[i].getName();  //moving through the array get the names of all files
            }
            
            if(fileList!= null){ //while the lise of files isn't null
                for(File file : fileList){  //so file : filelist is the outcome of what happens
                    System.out.println(file.getName()); // get the file name and print it into the console
                    String intocombobox = file.getName(); // 
                    jComboBox1.addItem(intocombobox);
                }
            }
        }
      private String[] dataFileToString(File inFile){
            String[] data = new String[50];
            String lastData;
            int i = 0;
            
            try {
                FileReader readIn = new FileReader(inFile);
                BufferedReader readFile = new BufferedReader(readIn);
                
                lastData = readFile.readLine();
                
                while(lastData != null && lastData != ""){
                    
                    if(i >= data.length){
                        
                        String[] temp = new String[data.length + 1];
                        
                        for(int j = 0; j < data.length; j++){
                            temp[j] = data[j];
                        }
                        
                        data = temp;
                        
                    }
                    
                    
                    data[i] = lastData;
                    lastData = readFile.readLine();
                    i++;
                }    
                
            } catch (IOException ex) {
            }
            
            return data;
        }
    The error message tells me that line 10 is a problem. What I don't understand is why? why is the void setup such an issue?


    this is from the application that works, if it helps. it's where I copied this void from
    Java Code:
     private void setup(){
            try {
                Properties prop = new Properties();
                FileReader read = new FileReader("filePath.properties");
                prop.load(read);
                folder = new File(prop.getProperty("testResourceFiles"));
                fileList = folder.listFiles();
            } catch (IOException ex) {
                System.out.println("File retrieval failed.");
            }
            
            files = new String[fileList.length];
            
            for(int i = 0; i < fileList.length; i++){
                files[i] = fileList[i].getName();
            }
            
            if(fileList!= null){
                for(File file : fileList){
                    System.out.println(file.getName());
                }
            }
        }
    The thing I don't understand is what's going on? why would I be directed to line 10 instead of where setup actually begins in line 23?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    The error message tells me that line 10 is a problem.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    the error reads

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.DefaultComboBoxModel.<init>(DefaultCom boBoxModel.java:58)
    at componentfillin.ComponentFilli.setup(ComponentFill i.java:54)
    at componentfillin.ComponentFilli.<init>(ComponentFil li.java:38)
    at componentfillin.ComponentFilli$16.run(ComponentFil li.java:492)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.DefaultComboBoxModel.<init>(DefaultCom boBoxModel.java:58)
    at componentfillin.ComponentFilli.setup(ComponentFill i.java:54)
    Look at line 54 and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid value. If needed use a print statement to print out the values of the variables to see which one is null.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: code won't put filenames into combobox?

    Did you sprinkle some print statements within the setup method? Somewhere something is returning a null reference which you are ignoring.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    Look at line 54 and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid value. If needed use a print statement to print out the values of the variables to see which one is null.

    line 54 is line 26 here. so it doesn't like files for some reason? I put files at the end of the code so every void could use it private String[] files;

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    What is in the variable: files? Does it have a non-null value?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: code won't put filenames into combobox?

    Have you got any print message for this

    files[i] = fileList[i].getName();..might be this is null
    Ramya:cool:

  9. #9
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    What is in the variable: files? Does it have a non-null value?
    Files just goes to a directory
    private File folder = new File("Z:\\adirectory\\someplace\\");
    private File[] fileList=folder.listFiles();
    private String[] files;

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    private String[] files;
    I was asking: Where is the variable: files assigned a value? What value does it have when the exception happens?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    I was asking: Where is the variable: files assigned a value? What value does it have when the exception happens?
    in setup files = new String[fileList.length];

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    Ok, now answer the next question:
    What value does the variable: files have when the exception happens?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    Ok, now answer the next question:
    What value does the variable: files have when the exception happens?
    I'm unsure as it won't print. I just tried println and nothing.
    Java Code:
     private void setup() throws IOException{
       
        
         jComboBox1.setModel(new DefaultComboBoxModel(fileList));
         fileList=folder.listFiles();
            files = new String[fileList.length];  
            System.out.println(files); // the only thing I added
            for(int i = 0; i < fileList.length; i++){ 
                files[i] = fileList[i].getName().toString();  
                jComboBox1.addItem(files);
            }
            
            if(fileList!= null){ 
                for(File file : fileList){  
                    System.out.println(file.getName()); 
                   String intocombobox = file.getName();  
                    jComboBox1.addItem(intocombobox);
                     jComboBox1.addItem(fileList.toString());
                }
            }
        }
    Last edited by innovo; 10-02-2015 at 03:06 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    it won't print. I just tried println and nothing.
    You need to print the value of the variable: files BEFORE the line where the exception happens. For example if the exception happens on line 54 the print statement needs to be immediately after line 53.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    You need to print the value of the variable: files BEFORE the line where the exception happens. For example if the exception happens on line 54 the print statement needs to be immediately after line 53.
    okay, I moved println to line 53 and I got null.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    I got null.
    That is the reason the program is getting the NullPointerException. Change the code so that the variable has a valid value BEFORE it is used.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    That is the reason the program is getting the NullPointerException. Change the code so that the variable has a valid value BEFORE it is used.
    alright, that's simple enough. Another problem I'm having is I get the filenames printed out in my console. It appears that they aren't in an array anymore. but I can't add them into the combobox for some reason via jComboBox1.addItem(fileList);

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    the filenames printed out in my console. It appears that they aren't in an array
    Please post the new code that shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: code won't put filenames into combobox?

    Quote Originally Posted by Norm View Post
    Please post the new code that shows the problem.
    Microsoft Windows [Version 10.0.10240]
    (c) 2015 Microsoft Corporation. All rights reserved.

    C:\Users\TEvans>java -jar C:\Users\TEvans\Desktop\ComponentFillin1.jar
    .DS_Store
    ComponentFillin.jar
    ComponentFillin1.jar
    EvansInterface.jar
    filePath.properties
    filePathcomponentFillin.properties
    lib
    TemplateGenerator.jar
    this.csv
    TomE.csv
    TRAVELERS
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv
    Z:\EvansInterface_Beta\TemplateGen\this.csv


    everything from ds.store to TRAVELERS are files and folders. The rest are some stupid print statement I put in there. You can see that everything is not listed in an array but as separate strings, yet when I try to add them as items it doesn't work.

    Java Code:
    private void setup() throws IOException{
       
      // System.out.println(files);
         jComboBox1.setModel(new DefaultComboBoxModel(fileList));//tells combobox to put the file names into the menu
         fileList=folder.listFiles();
            files = new String[fileList.length];  // files equals the length of filelist
          //  System.out.print(files);
            for(int i = 0; i < fileList.length; i++){ //for i=0; i< the length of file list; add 1 to i
                files[i] = fileList[i].getName();  //moving through the array get the names of all files and convert it to string
                jComboBox1.addItem(files);
                
               // System.out.print(files);
            }
            
            if(fileList!= null){ //while the lise of files isn't null
                for(File file : fileList){  //so file : filelist is the outcome of what happens
                    System.out.println(file.getName()); // get the file name and print it into the console
                   String intocombobox = file.getName(); // 
                    jComboBox1.addItem(intocombobox);
                     jComboBox1.addItem(fileList);
                }
            }
        }
    I'm focusing on line 10 and lines 19 and 20
    Last edited by innovo; 10-02-2015 at 06:12 PM.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: code won't put filenames into combobox?

    when I try to add them as items it doesn't work
    Can you explain what "doesn't work" means?
    Are there error messages? Copy the full text and paste it here.
    Is the contents of the list not what you want? Copy the contents and paste it here, or at least describe what is in the list.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. iterate through filenames in directory
    By jhoang in forum New To Java
    Replies: 2
    Last Post: 06-10-2013, 08:54 PM
  2. Continuing Filenames
    By Stamoulohta in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:37 PM
  3. Replies: 0
    Last Post: 03-17-2011, 12:07 PM
  4. Display filenames in frame
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2010, 02:51 PM
  5. Replies: 5
    Last Post: 01-27-2010, 09:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •