I can't figure out how to fix this null pointer exception and i've been going at it for a while now. Any help would be appreciated. I'm still a noob at this so go easy on some of the ways I did stuff, however I would love any tips that you guys have to offer.
class 1
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class VGrepGUI extends JFrame{ static JTextField inputTextField; static JTextField searchField; static JTextField currentFileField; static JTextField outputSectionField; JTextArea textArea; JScrollPane scrollPane; static LayoutManager MyLayout; VGrep functions = new VGrep(); public static void main(String[] args) { VGrepGUI grepTool = new VGrepGUI(); JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); //mainPanel.setLayout(); frame.setTitle("VGrep Tool");//set frame properties frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); mainPanel.add(grepTool.inputFileSection());//call on createGUI to retrieve jpanel to create GUI and add it to the JFrame mainPanel.add(grepTool.searchCriteriaSection()); mainPanel.add(grepTool.currentFileDisplay()); mainPanel.add(grepTool.scrollWindow()); mainPanel.add(grepTool.outputButtonSection()); frame.add(mainPanel); frame.pack(); frame.setSize(750, 750); } public JPanel inputFileSection(){ JPanel inputPanel = new JPanel(); JLabel inputFileLabel = new JLabel("Input File:"); inputTextField = new JTextField("Select a file to search...", 35); inputTextField.addActionListener(new inputTextFieldListener()); inputTextField.addFocusListener(new inputTextFieldFocusListener()); JButton browseForInputFile = new JButton("Browse"); browseForInputFile.addActionListener(new browseInputListener()); inputPanel.add(inputFileLabel); inputPanel.add(inputTextField); inputPanel.add(browseForInputFile); return inputPanel; }// end inputFileSection method public JPanel searchCriteriaSection(){ JPanel searchPanel = new JPanel(); JLabel searchLabel = new JLabel("Search For:"); searchField = new JTextField("Enter search criteria here...", 35); searchField.addActionListener(new searchFieldListener()); searchField.addFocusListener(new searchFieldFocusListener()); JButton searchButton = new JButton("Start Search"); searchButton.addActionListener(new searchButtonListener()); searchPanel.add(searchLabel); searchPanel.add(searchField); searchPanel.add(searchButton); return searchPanel; } public JPanel currentFileDisplay(){ JPanel fileDisplayPanel = new JPanel(); JLabel currentFileLabel = new JLabel("Searching through file:"); currentFileField = new JTextField(40); currentFileField.setEditable(false); fileDisplayPanel.add(currentFileLabel); fileDisplayPanel.add(currentFileField); return fileDisplayPanel; } /*public JTextArea getArea(){ textArea = new JTextArea(600,550); return textArea; }*/ public JPanel scrollWindow(){ JPanel scrollPanel = new JPanel(); textArea = new JTextArea(30,60); scrollPane = new JScrollPane(textArea, scrollPane.VERTICAL_SCROLLBAR_ALWAYS, scrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPanel.add(scrollPane); return scrollPanel; } public JPanel outputButtonSection(){ JPanel outputPanel = new JPanel(); JLabel outputLabel = new JLabel("Output File:"); outputSectionField = new JTextField(35); JButton browse = new JButton("Browse"); browse.addActionListener(new browseOutputListener()); JButton save = new JButton("Save"); save.addActionListener(new saveButtonListener()); outputPanel.add(outputLabel); outputPanel.add(outputSectionField); outputPanel.add(browse); outputPanel.add(save); return outputPanel; }//end outputButtonSection //action listener classes class inputTextFieldListener implements ActionListener{ public void actionPerformed(ActionEvent e){ } }//end inputTextFieldListener class browseInputListener implements ActionListener{ public void actionPerformed(ActionEvent e){ functions.browseFiles(); } }//end browseInputListener class searchFieldListener implements ActionListener{ public void actionPerformed(ActionEvent e){ } }//end searchFieldListener class searchButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ functions.searchThroughFile(); } }//end searchButtonListener class browseOutputListener implements ActionListener{ public void actionPerformed(ActionEvent e){ functions.browseSaveFile(); } }//end browseOutputListener class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ } }//end saveButtonListener //focus listeners class inputTextFieldFocusListener implements FocusListener{ public void focusLost(FocusEvent a){ String str2 = inputTextField.getText(); if(str2.equals("")){ inputTextField.setText("Select a file to search..."); } } public void focusGained(FocusEvent b){ String str = inputTextField.getText(); if(str.equals("Select a file to search...")){ inputTextField.setText(""); } } }//end focuslistener for inputtextfield class searchFieldFocusListener implements FocusListener{ public void focusLost(FocusEvent a){ String str2 = searchField.getText(); if(str2.equals("")){ searchField.setText("Enter search criteria here..."); } } public void focusGained(FocusEvent b){ String str = searchField.getText(); if(str.equals("Enter search criteria here...")){ searchField.setText(""); } } } }//end VGrepGUI class
class 2:
import javax.swing.*; import java.awt.*; import java.io.*; import java.util.*; public class VGrep { static boolean searching; static VGrepGUI gui = new VGrepGUI(); JFileChooser fc; static File inputFile; StringBuffer sbuffer = new StringBuffer(); BufferedReader reader = null; private final static String newline = "\n"; public static void main(String[] args){ VGrep grepFunctions = new VGrep(); } public void browseFiles(){ fc = new JFileChooser(); if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { inputFile = fc.getSelectedFile(); gui.inputTextField.setText("" + inputFile.getAbsolutePath()); } }//end browse files public void searchingFileBox(){ while(searching == true){ gui.currentFileField.setText("" + inputFile.getName()); } }//end searchingFileBox public String getSearchCriteria(){ String str = gui.searchField.getText(); return str; } public void searchThroughFile(){ if(inputFile != null){ try{ //open scanner to read file and eventually write to textarea Scanner in = new Scanner(inputFile); while(in.hasNextLine()){ String line = in.nextLine(); processString(line); } in.close(); }catch(Exception e){ System.out.println(e); } }else browseFiles(); }//end searchThroughFile public void processString(String str1){ String str = getSearchCriteria(); if(str1.contains(str)){ printToTextArea(str1); } }//end processString public void printToTextArea(String str){ gui.textArea.append("" + str + newline); }//end printToTextArea public void browseSaveFile(){ if(fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION); }//end browseSaveFile public void saveFile(){ } }