Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Storing data from a file in array and setting it to textbox

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Storing data from a file in array and setting it to textbox

    Ok so basicly i have created 3 string arrays(arrFileName, arrFilePci, arrFileIp) Each is for seperate string values from the file.

    Once im clicking on the Find Entry button it is generating a huge error list which i really wouldnt have a clue as to where to start looking, I am then using an IF statement to check if the word entered in the textbox exists in the text file, If it does set the 3 text boxes in my main class to the 3 array data entrys on that line.

    Here is the error log:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at searchFile.actionPerformed(searchFile.java:85)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6289)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6054)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4652)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4482)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4482)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:603)
    	at java.awt.EventQueue$1.run(EventQueue.java:601)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:617)
    	at java.awt.EventQueue$2.run(EventQueue.java:615)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    here is the code for the find entry:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
     
    /**
     * Write a description of class searchFile here.
     * 
     * @author Mark Klesnik 
     * 1.0
     */
    public class searchFile implements ActionListener
    {
     
    JButton btnDispose, btnFinder;
     
    public Scanner scanReader;   
    public static int intArrCounter;
    public static String [] arrFileName; 
    public static String [] arrFilePci;
    public static String [] arrFileIp;
    public JTextField txtFindStr;
    public JFrame frmSearch;
    public void strFinder(){
     
    String arrFileName[] = new String[50];
    String arrFilePci[] = new String[50];
    String arrFileIp[] = new String[50];
     
    frmSearch = new JFrame("Search for a client");
    frmSearch.setVisible(true);
    frmSearch.setSize(400, 100);
    frmSearch.setResizable(false);
     
    JPanel panel2 = new JPanel();
    panel2.setVisible(true);
    panel2.setSize(400, 200);
    panel2.setBackground(Color.BLACK);
     
    frmSearch.add(panel2);
     
     
    JLabel findstr = new JLabel("Enter the client name:  ");
    findstr.setForeground(Color.WHITE);
     
    txtFindStr = new JTextField(15);
    txtFindStr.setForeground(Color.GREEN);
    txtFindStr.setBackground(Color.BLACK);
     
    btnFinder = new JButton(" Find Entry ");
    btnFinder.setBackground(Color.BLACK);
    btnFinder.setForeground(Color.GREEN);
     
    btnDispose = new JButton(" Cancel ");
    btnDispose.setBackground(Color.BLACK);
    btnDispose.setForeground(Color.RED);
     
    panel2.add(findstr);
    panel2.add(txtFindStr);
    panel2.add(btnFinder);
    panel2.add(btnDispose);
     
     
    btnDispose.addActionListener(this);
    btnFinder.addActionListener(this);
    }
     
     @Override
    public void actionPerformed(ActionEvent events) 
        {
            //project.stringOne = txtFindStr.getText();
             if (btnDispose.hasFocus())  { frmSearch.dispose(); }
     
             if (btnFinder.hasFocus()) {  
                  intArrCounter = 0;
     
                  try{
                      scanReader = new Scanner(new File("ipaddress.txt"));
                    } catch(Exception error) {
                        System.out.println("Sorry, Could not find the file specified.");
                    }
     
                  while(scanReader.hasNext()){
                      arrFileName[intArrCounter] = scanReader.next();
                      arrFilePci[intArrCounter] = scanReader.next();
                      arrFileIp[intArrCounter] = scanReader.next();
     
                      intArrCounter++;
                    }
                    if (txtFindStr.getText() == arrFileName[intArrCounter]){
     
                        project.txtName.setText(arrFileName[intArrCounter]);
                        project.txtPCI.setText(arrFilePci[intArrCounter]);
                        project.txtIP.setText(arrFileIp[intArrCounter]);
                    }
     
                  scanReader.close();
                 frmSearch.dispose();            
                }
        }
     
     
    }

    Thanks in advance, If i forgot to mention something clearly please let me know im frustrated trying to figure it out


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,931
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Storing data from a file in array and setting it to textbox

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at searchFile.actionPerformed(searchFile.java:85)
    What variable at line 85 is null? If you can't easily see it, add a print statement before line 85 to print out all the variables used on line 85 to see which one is null.

  3. The Following User Says Thank You to Norm For This Useful Post:

    macko (May 13th, 2011)

  4. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Storing data from a file in array and setting it to textbox

    ah ok so thats what the nullPointer exception means, Thank you very much sir

    Seems to be my string array getting data from the file

    while(scanReader.hasNext()){
    line 85 arrFileName[intArrCounter] = scanReader.next();
    arrFilePci[intArrCounter] = scanReader.next();
    arrFileIp[intArrCounter] = scanReader.next();

    intArrCounter++;
    }

    scanReader is what i called my scanner to read data in a file, Its then initializing the first peice of data to the arrfilename the second to Pci and the third to Ip. as far as i see it the arra must be null to begin with in order to scan the file and store data in it. The program will rescan the file everything the code is executed and whille always start as null, I will try to add a string to [0] see if it works.
    Last edited by macko; May 13th, 2011 at 07:26 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,931
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Storing data from a file in array and setting it to textbox

    Did you have a further question? I don't see it.

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Storing data from a file in array and setting it to textbox

    lol.. sorry thinking out loud as usual

    Just going to try a different method of storing data from a textfile to a array.

Similar Threads

  1. Storing User Data
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: January 21st, 2011, 09:07 AM
  2. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 10:43 AM
  3. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 10:16 AM
  4. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  5. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM