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 8 of 8

Thread: Nullpointerexception

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nullpointerexception

    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(){
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointerexception

    By the way, I forgot to mention that the Nullpointerexception occurs after I select a file via browse button, type in what i'm searching for, and hit start search button.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Nullpointerexception

    So this is where you are getting the problem?
    JButton browse = new JButton("Browse");
            browse.addActionListener(new browseOutputListener());
    And since browseOutputListener calls:
    class browseOutputListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                functions.browseSaveFile();
            }
        }//end browseOutputListener
    which calls:
    public void browseSaveFile(){
            if(fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION);
        }//end browseSaveFile
    Which is a broken method effectively. You have an if statement that does nothing. I would guess that is part of your problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Nullpointerexception

    Quote Originally Posted by kbwalker87 View Post
    By the way, I forgot to mention that the Nullpointerexception occurs after I select a file via browse button, type in what i'm searching for, and hit start search button.
    Please post the full NullPointerException error message. An exception message will point you to the exact point that the exception is thrown, which makes a world of difference in determining what is causing the error.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointerexception

    wow, i guess I have overlooked that. I can't thank you enough. I'll let you know how it works out.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointerexception

    Ok, that actually did not fix it, and I forgot that I haven't made it to that point in the program which is why I left it like that. And thanks copeg, but unfortunately it doesn't tell me where this is going on at which I thought was really strange. It shows up in the console window at the bottom and just says "java.lang.NullPointerException".

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointerexception

    If anyone is still following this thread at all, the problem is occurring in the "printToTextArea" method. I guess it has something to do with my reference to the JTextArea?

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Nullpointerexception

    Take a look at this Common Java Mistakes - Forgetting to initialize a variable.

    Most NullPointExceptions happen because of this reason (I'd say maybe ~80% of the time, the rest is from output that is suppose to/can be null and the code doesn't take that into consideration).

Similar Threads

  1. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  2. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM
  3. Funny NullPointerException
    By Marcus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2009, 10:14 AM
  4. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM